Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: kohlanta on March 05, 2013, 09:56:25 PM



Title: [solved] http error from json-rpc in php
Post by: kohlanta on March 05, 2013, 09:56:25 PM
I am using the json-rpc php library from the bitcoin wiki (http://jsonrpcphp.org/) to send commands to my bitcoind instance.  For the most part, it works as expected, but occasionally, when I use the sendtoaddress command, it will return one of the following errors:
Code:
fopen(http://...@localhost:8334/): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
or
Code:
fopen(http://...@localhost:8334/): failed to open stream: HTTP request failed!
I noticed that the first one seems to happen only when there are insufficient funds in the wallet.  The second seems to happen at random.  When the first occurs, no coins are sent.  When the second occurs, the coins are almost always sent just as if there was no error even though it seems, based on the error, that the command never made it to bitcoind.  Occasionally though, the second error will be returned and the coins aren't sent.

This ambiguity is a problem for me.  What I would really like is for the jsonrpc client to return a meaningful error when it cannot process the request (such as insufficient funds) and to return the usual output (txid) when sendtoaddress is successfully executed.  I'm most comfortable in php, but am willing to learn other languages to achieve the same ends.  Ultimately I need to take addresses/amounts from a mysql database and construct transactions in my bitcoind server.

So, if anyone knows how to make this work in php or another language, perhaps perl or ruby or python and can walk me though it, and it works as described, 5 btc will be yours.


Title: Re: [5 btc bounty] http error from json-rpc in php
Post by: Anduck on March 05, 2013, 10:08:26 PM
Try setting stream_context timeout to longer time, might help.

Or try cURL!

And try taking the http:// outta there.

And if it helps, here's an example on how to do it:
http://pastebin.com/YCYb0nPv (http://pastebin.com/YCYb0nPv)

Let me know if any of these works.


Title: Re: [5 btc bounty] http error from json-rpc in php
Post by: kohlanta on March 06, 2013, 12:16:55 AM
Try setting stream_context timeout to longer time, might help.

Or try cURL!

And try taking the http:// outta there.

And if it helps, here's an example on how to do it:
http://pastebin.com/YCYb0nPv (http://pastebin.com/YCYb0nPv)

Let me know if any of these works.

Great ideas.  I'll play around with this and get back to you.


Title: Re: [5 btc bounty] http error from json-rpc in php
Post by: GoldenWings91 on March 06, 2013, 04:10:31 AM
I'm not sure about the second error but your first is caused when bitcoin returns an error. json-rpc can't handle errors and will throw an exception which would stop your entire script. When you try to send coins if there are insufficent funds bitcoin will return an error which will cause json to throw an exception. In this case you should include checks:
Quote
$balance = $bitcoind->getbalance(<account>);
$fee = 0.0005;

if($balance - $fee >= .00000001)
                   $bitcoind->sendto();
else echo "Insufficient Funds";


Title: Re: [5 btc bounty] http error from json-rpc in php
Post by: gweedo on March 06, 2013, 04:13:56 AM
Your getting that error cause your script can't talk to your bitcoind, make sure you use the allowip, make sure you have the server=1, and rpcuser and rpcpass commands in your config file.


Title: Re: [5 btc bounty] http error from json-rpc in php
Post by: kohlanta on March 06, 2013, 08:06:28 AM
Try setting stream_context timeout to longer time, might help.

Or try cURL!

And try taking the http:// outta there.

And if it helps, here's an example on how to do it:
http://pastebin.com/YCYb0nPv (http://pastebin.com/YCYb0nPv)

Let me know if any of these works.

curl solved all of my problems!  got the code I needed here: http://pastebin.com/vREuHVr5
where would you like the 5 btc sent?


Title: Re: [solved] http error from json-rpc in php
Post by: Xenland on March 06, 2013, 08:07:32 AM
Perhaps my PHP BDK library might help http://bitcoindevkit.com