Sign in to follow this  
Followers 0
zelles

Simplified PHP RPC communication [zellesRPC.php]

2 posts in this topic

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

I need help from our community on how to get virtacoin Rpc, can the current one be use for virtacoin or can we do anything. as we can see it is compactable with so many coin. can it also work for virtacoin?


Thanks


Able Digital


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  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.