Author [EN] [ZH] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] [EN] [ZH] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] [EN] [ZH] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] Topic: RPC- I Need A Working PHP Example  (Read 1498 times)

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
RPC- I Need A Working PHP Example
« 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.
« Last Edit: September 27, 2014, 05:50:14 AM by erick »
BTSX account: erick

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 11951
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BTS: xeroc
  • GitHub: xeroc
Re: RPC- PHP Example
« Reply #1 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
})
Give BitShares a try! Use the http://testnet.bitshares.eu provided by http://bitshares.eu powered by ChainSquad GmbH

Offline svk

Re: RPC- PHP Example
« Reply #2 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);
Worker: dev.bitsharesblocks

Offline bytemaster

Re: RPC- PHP Example
« Reply #3 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:[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: 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 erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: RPC- PHP Example
« Reply #4 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://******:[email protected]: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 xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 11951
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BTS: xeroc
  • GitHub: xeroc
Re: RPC- I Need A Working PHP Example
« Reply #5 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:[email protected]:19988/rpc

the "/rpc" at the url is IMPORTANT!
Give BitShares a try! Use the http://testnet.bitshares.eu provided by http://bitshares.eu powered by ChainSquad GmbH

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: RPC- I Need A Working PHP Example
« Reply #6 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:[email protected]: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: 11951
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BTS: xeroc
  • GitHub: xeroc
Give BitShares a try! Use the http://testnet.bitshares.eu provided by http://bitshares.eu powered by ChainSquad GmbH

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: RPC- I Need A Working PHP Example
« Reply #8 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.

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

Offline erick

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: RPC- I Need A Working PHP Example
« Reply #9 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. :-\ :-\
BTSX account: erick

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 11951
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BTS: xeroc
  • GitHub: xeroc
Re: RPC- I Need A Working PHP Example
« Reply #10 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!!
Give BitShares a try! Use the http://testnet.bitshares.eu provided by http://bitshares.eu powered by ChainSquad GmbH

Offline biophil

  • Hero Member
  • *****
  • Posts: 774
  • Incentives run the world
    • View Profile
  • BTS: zebulon
Re: RPC- I Need A Working PHP Example
« Reply #11 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.

Offline arubi

  • Sr. Member
  • ****
  • Posts: 209
    • View Profile
Re: RPC- I Need A Working PHP Example
« Reply #12 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

Offline biophil

  • Hero Member
  • *****
  • Posts: 774
  • Incentives run the world
    • View Profile
  • BTS: zebulon
Re: RPC- I Need A Working PHP Example
« Reply #13 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


 

Google+