Jump to content

Simplified PHP RPC communication [zellesRPC.php]

Started By zelles , Dec 29 2014 09:21 PM
php json rpc bitcoin franko communicate wallet script zellesRPC.php zellesRPC zelles

  • Please log in to reply
1 reply to this topic

#1

zelles
  • zelles
  • Full Member
  • MEMBER
  • PipPip
  • 160 posts
  • 24 topics
    • Time Online: 5d 18h 26m 34s
  • Country:United States
  • LocationUnder Water

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;
}
?>

  • 0

Cryptocointalk.com doesn't endorse any ads. Warning: ads my be illegal or untrustworthy in your jurisdiction. Advertising Here.

#2

Emeka Okoli
  • Emeka Okoli
  • Newbie
  • MEMBER
  • Pip
  • 1 posts
  • 0 topics
    • Time Online: 28m 7s
  • Country:Nigeria

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


  • 0





Also tagged with one or more of these keywords: php, json, rpc, bitcoin, franko, communicate, wallet, script, zellesRPC.php, zellesRPC, zelles

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users