BitShares Forum

Main => Technical Support => Topic started by: erick on September 10, 2014, 05:07:44 am

Title: RPC- I Need A Working PHP Example
Post by: erick on September 10, 2014, 05:07:44 am
I am in need of a working PHP example. I want to start a website that can send and receive btsx and bitusd but everything i have tried has failed.
Title: Re: RPC- PHP Example
Post by: xeroc on September 10, 2014, 08:19:38 am
not sure how to do it in PHP but you can take a look at the RPC implemention with python:
https://github.com/xeroc/pytshares/blob/master/btsrpcapi.py

relevant part:
Code: [Select]
rpcexec({
"method": "get_info",
"params": [],
"jsonrpc": "2.0",
"id": 0
})
Title: Re: RPC- PHP Example
Post by: svk on September 10, 2014, 08:48:01 am
Don't know about PHP either but in nodejs I'm using a module called jayson that does jsonrpc 2.0 by default:

Code: [Select]
var jayson = require('jayson');
var jaysonClient = jayson.client.http({
    port:3001,
    hostname: '127.0.0.1',
    path: '/rpc',
    auth:'username:password'
});

This can then be used via:

Code: [Select]
jaysonClient.request('get_info',[],callback);
Title: Re: RPC- PHP Example
Post by: bytemaster on September 10, 2014, 05:29:12 pm
Quote
PHP

The JSON-RPC PHP library also makes it very easy to connect to Bitcoin. For example:

Code: [Select]
  require_once 'jsonRPCClient.php';
 
  $bitcoin = new jsonRPCClient('http://user:password@127.0.0.1:8332/');
 
  echo "<pre>\n";
  print_r($bitcoin->getinfo()); echo "\n";
  echo "Received: ".$bitcoin->getreceivedbylabel("Your Address")."\n";
  echo "</pre>";

You can use the $bitcoin object to talk to BTSX.
Title: Re: RPC- PHP Example
Post by: erick on September 27, 2014, 05:33:18 am
Here is my code, It works with bitcoin just fine , if i change the port to my bitcoin port it connects no problem, but with Bitsharesx it does not work , please someone tell me the secrets to working with btsx.
Code: [Select]
  <?php
  error_reporting
(E_ALL);
  
date_default_timezone_set('America/Detroit');
   require_once 
'jsonRPCClient.php';
  
 
$bts = array("user" => "*******(omitted)**********",             // RPC Username
              
"pass" =>   "********(omitted)*********",                   // RPC Password
              
"host" =>   "127.0.0.1",                      // RPC Hostname/IP
              
"port" =>   8765);                            // RPC Port - I am running bitsharesx on port 8765 for http

$mybts = new jsonRPCClient("http://{$bts['user']}:{$bts['pass']}@{$bts['host']}:{$bts['port']}");
 
echo
"<br>";
print_r($mybts->getinfo());
echo
"<br>";


  
?>


and this is the error i get


Warning: fopen(http://...@127.0.0.1:87): failed to open stream: HTTP request failed! HTTP/1.1 400 Connection: close in C:\ProgramsDev1\Php_551\includes\jsonRPCClient.php on line 132

Fatal error: Uncaught exception 'Exception' with message 'Unable to connect to http://******:*****@127.0.0.1:87' in C:\ProgramsDev1\Php_551\includes\jsonRPCClient.php:140 Stack trace: #0 C:\ProgramsDev1\My_WWW\testRPC.php(17): jsonRPCClient->__call('getinfo', Array) #1 C:\ProgramsDev1\My_WWW\testRPC.php(17): jsonRPCClient->getinfo() #2 {main} thrown in C:\ProgramsDev1\Php_551\includes\jsonRPCClient.php on line 140




the rpc username and password have been ommitted , they are correct in the running version . :)
Title: Re: RPC- I Need A Working PHP Example
Post by: xeroc on September 27, 2014, 09:40:33 am
try to use 'curl' to access tha api and check the settings:
Code: [Select]
curl --data '{"method":"about", "params": [], "json-rpc": 2.0, "id": 0}' http://USER:PASSWORD@localhost:19988/rpc

the "/rpc" at the url is IMPORTANT!
Title: Re: RPC- I Need A Working PHP Example
Post by: erick on September 28, 2014, 12:01:07 am
try to use 'curl' to access tha api and check the settings:
Code: [Select]
curl --data '{"method":"about", "params": [], "json-rpc": 2.0, "id": 0}' http://USER:PASSWORD@localhost:19988/rpc

the "/rpc" at the url is IMPORTANT!


When I use my port and name and password to run the command it gives me this error.



curl: (6) Could not resolve host: params
curl: (3) [globbing] illegal character in range specification at pos 2
curl: (6) Could not resolve host: json-rpc
curl: (6) Could not resolve host: 2.0,
curl: (6) Could not resolve host: id
curl: (3) [globbing] unmatched close brace/bracket at pos 2
Invalid RPC Request
4 parse_error_exception: Parse Error
Unexpected char '39' in ""
    {"c":39,"s":""}
    th_a  json.cpp:411 fc::variant_from_stream

    {"str":"'{method:about,"}
    th_a  json.cpp:421 fc::json::from_string
Title: Re: RPC- I Need A Working PHP Example
Post by: xeroc on September 28, 2014, 09:58:08 am
curl: (6) Could not resolve host: params
you probobly missed some ' or "  ... check your command .. maybe check without data if the server responds:

curl http://USER:PASSWORD@localhost:19988/rpc

if you have a blank in user or password you might want try

curl "http://USER:PASSWORD@localhost:19988/rpc"
Title: Re: RPC- I Need A Working PHP Example
Post by: erick on September 29, 2014, 11:12:59 pm
ok when i start bitshares_client it says this


Starting JSON RPC server on port 8766 (localhost only)
Starting HTTP JSON RPC server on port 8765 (localhost only)

when I try the curl command I get this

Invalid RPC Request
11 eof_exception: End Of File
stringstream
    {}
    th_a  sstream.cpp:109 fc::stringstream::peek

    {"str":""}
    th_a  json.cpp:421 fc::json::from_string


when I try to connect via php I get this

Warning: fopen(http://...@127.0.0.1:8765): failed to open stream: HTTP request failed! HTTP/1.1 400 Connection: close in C:\ProgramsDev1\Php_551\includes\jsonRPCClient.php on line 132

and if i run netstat -anb I can see that bitsharesx is listening on port 8765 and 8766

but the same code works with bitcoin just fine , so i am totally confused.

Title: Re: RPC- I Need A Working PHP Example
Post by: erick on September 30, 2014, 01:29:29 am
I know its not much but i sent everyone that responded 10 btsx for trying to help me out, but my problems are still here. I might have to start over from scratch and maybe i'll solve it. again ty to everyone for trying. :-\ :-\
Title: Re: RPC- I Need A Working PHP Example
Post by: xeroc on September 30, 2014, 07:14:01 am
the curl command was a console command .. but as you have windows you wont have curl installed..

BUT would you please add "/rpc" to your URL .. after the port!!
Title: Re: RPC- I Need A Working PHP Example
Post by: biophil on November 08, 2014, 07:46:55 pm
Hey erick, did you ever get this working? I see this thread was from a while ago, but if you're still around I'd love to see what you learned.
Title: Re: RPC- I Need A Working PHP Example
Post by: arubi on November 09, 2014, 09:38:28 am
Hey erick, did you ever get this working? I see this thread was from a while ago, but if you're still around I'd love to see what you learned.

This is what I'm doing:

1. Run `bitshares_client` with the `--server` and `--httpport` flags set:

Code: [Select]
$ ./bitshares_client --server --httpport 9989
2. To communicate with json-rpc, use curl:

Code: [Select]
curl --user `grep rpc_user ~/.BitSharesX/config.json | cut -d ":" -f 2 | tr -d " \",\n"`:`grep rpc_password ~/.BitSharesX/config.json | cut -d ":" -f 2 | tr -d " \",\n"` --data-binary '{"method":"about", "params": [], "json-rpc": 2.0, "id": 0}' -H 'content-type: text/plain;' http://localhost:9989/rpc
Title: Re: RPC- I Need A Working PHP Example
Post by: biophil on November 09, 2014, 05:05:42 pm
Hey erick, did you ever get this working? I see this thread was from a while ago, but if you're still around I'd love to see what you learned.

This is what I'm doing:

1. Run `bitshares_client` with the `--server` and `--httpport` flags set:

Code: [Select]
$ ./bitshares_client --server --httpport 9989
2. To communicate with json-rpc, use curl:

Code: [Select]
curl --user `grep rpc_user ~/.BitSharesX/config.json | cut -d ":" -f 2 | tr -d " \",\n"`:`grep rpc_password ~/.BitSharesX/config.json | cut -d ":" -f 2 | tr -d " \",\n"` --data-binary '{"method":"about", "params": [], "json-rpc": 2.0, "id": 0}' -H 'content-type: text/plain;' http://localhost:9989/rpc

Thanks for the tip! I never did get my curl command formatted correctly, but I finally did figure out how to get python talking to bitshares.

Also thanks to xeroc for his python code examples; they were an invaluable help.

Sent from my SCH-S720C using Tapatalk 2