Author Topic: RPC- I Need A Working PHP Example  (Read 7089 times)

0 Members and 1 Guest are viewing this topic.

Offline biophil

  • Hero Member
  • *****
  • Posts: 880
  • Professor of Computer Science
    • View Profile
    • My Academic Website
  • BitShares: biophil
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

Support our research efforts to improve BitAsset price-pegging! Vote for worker 1.14.204 "201907-uccs-research-project."

Offline arubi

  • Sr. Member
  • ****
  • Posts: 209
    • View Profile
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

Offline biophil

  • Hero Member
  • *****
  • Posts: 880
  • Professor of Computer Science
    • View Profile
    • My Academic Website
  • BitShares: biophil
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.
Support our research efforts to improve BitAsset price-pegging! Vote for worker 1.14.204 "201907-uccs-research-project."

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
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!!

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
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. :-\ :-\
BTSX account: erick

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
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.

« Last Edit: September 30, 2014, 01:11:07 am by erick »
BTSX account: erick

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
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"

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
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
BTSX account: erick

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
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!

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
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 . :)
« Last Edit: September 29, 2014, 11:15:22 pm by erick »
BTSX account: erick

Offline bytemaster

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.
For the latest updates checkout my blog: http://bytemaster.bitshares.org
Anything said on these forums does not constitute an intent to create a legal obligation or contract between myself and anyone else.   These are merely my opinions and I reserve the right to change them at any time.

Offline svk

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);
Worker: dev.bitsharesblocks

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
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
})

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
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.
« Last Edit: September 27, 2014, 05:50:14 am by erick »
BTSX account: erick