RPC- I Need A Working PHP Example

Started by erick, September 10, 2014, 05:07:44 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

biophil

Quote from: arubi on November 09, 2014, 09:38:28 AM
Quote from: 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.

This is what I'm doing:

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

$ ./bitshares_client --server --httpport 9989

2. To communicate with json-rpc, use curl:

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."

arubi

Quote from: 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.

This is what I'm doing:

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

$ ./bitshares_client --server --httpport 9989

2. To communicate with json-rpc, use curl:

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

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."

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!!

erick

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

erick

#9
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://[email protected]: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.

BTSX account: erick

xeroc

Quote from: erick on September 28, 2014, 12:01:07 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"

erick

Quote from: xeroc on September 27, 2014, 09:40:33 AM
try to use 'curl' to access tha api and check the settings:

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

xeroc

try to use 'curl' to access tha api and check the settings:

curl --data '{"method":"about", "params": [], "json-rpc": 2.0, "id": 0}' http://USER:PASSWORD@localhost:19988/rpc


the "/rpc" at the url is IMPORTANT!

erick

#5
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.

  <?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://[email protected]: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 . :)
BTSX account: erick

bytemaster

QuotePHP

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

  require_once 'jsonRPCClient.php';

  $bitcoin = new jsonRPCClient('http://user:[email protected]: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: [url="http://bytemaster.bitshares.org"]http://bytemaster.bitshares.org[/url]
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.

svk

Don't know about PHP either but in nodejs I'm using a module called jayson that does jsonrpc 2.0 by default:

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:

jaysonClient.request('get_info',[],callback);
Worker: dev.bitsharesblocks

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:

rpcexec({
"method": "get_info",
"params": [],
"jsonrpc": "2.0",
"id": 0
})

erick

#1
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.
BTSX account: erick