Jump to content

NEWS
  • WELCOME TO CRYPTOCURRENCYTALK!
  • We've upgraded the website with a lot of new features!
  • 3 NEW THEMES! Click on the + Themes button above!
  • Notice about SOCIAL LINKS: UPDATE YOUR SOCIAL LINKS
  • New video section, add your videos: VIDEO SECTION
  • Advertising will be available shortly, hold tight.
  • If you have any issues with the new site, please submit a support ticket: SUPPORT
Sign in to follow this  
zelles

Simplified PHP RPC communication [zellesRPC.php]

Recommended Posts

Here is a simple PHP script I wrote to easily communicate with, crypto-coin, wallet servers using php. You can use this script for local or remote RPC communication and make the API calls located at https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list. This script is compatible with Bitcoin, Litecoin, Franko, Dogecoin, etc...

 

Example usage:

<?php
require'zellesRPC.php'

$frk_creds = RPCNew('Server','RPCPort','RPCUser','RPCPassword');

$response = RPCRun($frk_creds,'getinfo','');

echo '<pre>';
print_r($response);
echo '</pre>';
?>

Source for: zellesRPC.php

<?php
/*
   Author:
     zelles http://gihub.com/zelles
   Prepare a clients credentials:
     $frk_creds = RPCNew('Server','RPCPort','RPCUser','RPCPassword');
   Making Calls:
     Passing no parameters:
       $response = RPCRun($frk_creds,'getinfo','');
     Passing one parameter:
       $response = RPCRun($frk_creds,'getaccount','F6u6YfPWuCa7QkZ3pFAxjtXdotjFxoZC8Q');
     Passing multiple parameters
       $response = RPCRun($frk_creds,'listtransactions','android, 10');
   Standard API calls:
     https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list
*/
function RPCNew($f_svr, $f_prt, $f_usr, $f_pwd) {
   return (object) array('rpcserver' => $f_svr,'rpcport' => $f_prt,'rpcuser' => $f_usr,'rpcpassword' => $f_pwd);
}
function RPCRun($f_client, $f_call, $f_parameters) {
   if($f_parameters == '') { $f_json = '{"method":"'.$f_call.'","params":[],"id":"'.$f_call.'"}'; }
   elseif(strpos($f_parameters,',') == false) { $f_json = '{"method":"'.$f_call.'","params":["'.$f_parameters.'"],"id":"'.$f_call.'"}'; }
   else {
      $f_setparameters = '';
      $f_params = explode(',', $f_parameters);
      foreach($f_params as $f_param) {
         if($f_setparameters == '') { if(is_numeric($f_param)) { $f_setparameters .= trim($f_param); } else { $f_setparameters .= '"'.trim($f_param).'"'; } } else { if(is_numeric($f_param)) { $f_setparameters .= ','.trim($f_param); } else { $f_setparameters .= ',"'.trim($f_param).'"'; } }
         $f_json = '{"method":"'.$f_call.'","params":['.$f_setparameters.'],"id":"'.$f_call.'"}';
      }
   }
   $f_c = curl_init('http://'.urlencode($f_client->rpcuser).':'.urlencode($f_client->rpcpassword).'@'.$f_client->rpcserver.':'.$f_client->rpcport.'/');
   curl_setopt($f_c, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($f_c, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
   curl_setopt($f_c, CURLOPT_POST, true);
   curl_setopt($f_c, CURLOPT_POSTFIELDS, $f_json);
   $f_responce = json_decode(curl_exec($f_c), true);
   curl_close($f_c);
   if($f_responce['result'] == '') { $f_rtn['error'] = $f_responce['error']; } else { $f_rtn = $f_responce['result']; }
   return $f_rtn;
}
?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using CRYPTOCURRENCYTALK.COM, you agree to our Terms of Use.