Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: sentry on May 09, 2011, 09:51:00 PM



Title: PHP JSON sendfrom function
Post by: sentry on May 09, 2011, 09:51:00 PM
Would someone mind taking a look at this, I can't figure out what I'm doing wrong, it seems like it shouldn't be this difficult. I think the problem is my typecasts, whether its a string, int, or float.

Code:
<?php
$bitcoin 
= new jsonRPCClient("http://".BC_USER.":".BC_PASS."@".BC_HOST.":".BC_PORT);

$to "1FN8m1bzfzqo761p4vAwU6Rb2vJD3dFs7a";

//print_r($bitcoin->listreceivedbyaccount());

foreach($bitcoin->listreceivedbyaccount() as $key=>$value){
if($bitcoin->getbalance((string)$value['account']) > "0")
$bitcoin->sendfrom((string)$value['account'], (string)$to, (float)$bitcoin->getbalance((string)$value['account']));
//echo "$value[account] => $value[amount]<br>";
}
?>


I get this error:
Code:
Warning: fopen(http://...@127.0.0.1:8332) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in \www\classes\jsonRPCClient.php on line 132

Fatal error: Uncaught exception 'Exception' with message 'Unable to connect to http://****:****@127.0.0.1:8332' in \www\classes\jsonRPCClient.php:140 Stack trace: #0 \www\bitcoin.php(20): jsonRPCClient->__call('sendfrom', Array) #1\www\bitcoin.php(20): jsonRPCClient->sendfrom(0, '1FN8m1bzfzqo761...', 3.07) #2 {main} thrown in \www\classes\jsonRPCClient.php on line 140

print_r shows this for $bitcoin->listreceivedbyaccount()

Code:
Array ( [0] => Array ( [account] => Mining [label] => Mining [amount] => 3.07 [confirmations] => 424 ) [1] => Array ( [account] => Personal [label] => Personal [amount] => 0.99 [confirmations] => 1021 ) [2] => Array ( [account] => f899139df5e1059396431415e770c6dd [label] => f899139df5e1059396431415e770c6dd [amount] => 0.3 [confirmations] => 873 ) )


Title: Re: PHP JSON sendfrom function
Post by: sentry on May 09, 2011, 11:04:37 PM
I have figured this out, I overlooked that the wallet has no balance to send anything from  ;D . I was able to use move though as the whole point of all this was to consolidate balances into one account. Although strangely enough I'm seeing ghost accounts that do not show up in the wallet but are listed by the RPC, oh well. For anyone following this and interested in the code, here ya go!

Code:
<?php
require_once dirname(__FILE__) . "/classes/jsonRPCClient.php";

define ("BC_HOST""127.0.0.1");
define ("BC_USER""***");
define ("BC_PASS""***");
define ("BC_PORT""8332");

$bitcoin = new jsonRPCClient("http://".BC_USER.":".BC_PASS."@".BC_HOST.":".BC_PORT);

$to "ACCOUNT_NAME";

print_r($bitcoin->listaccounts());

echo 
"<hr>";

foreach(
$bitcoin->listaccounts() as $key=>$value){
if($bitcoin->getbalance((string)$key) > "0")
$bitcoin->move((string)$key, (string)$to, (float)$value);
echo "$key => ".(float)$value."<br>";
}
?>


This simply moves all balances from accounts greater than 0 into the account you tell it to. I would really like to settle the negative accounts somehow, but I'll get into that soon. Needed this because on my bank project I have been working on my account shows I have a value > 0, but the wallet total shows 0...


Title: Re: PHP JSON sendfrom function
Post by: Alex Beckenham on May 19, 2011, 06:44:06 PM
I've been trying to find documentation on what the sendfrom function actually returns...

True or false or something more detailed?

Code:
try {
$bitcoin->sendfrom($acc,$addr,(float)$amt);
} catch (Exception $e) {
die($e);
}

Edit: Okay just did a test and it appears to return a txid... is this documented anywhere handy yet? Had no luck searching for it in Google.


Title: Re: PHP JSON sendfrom function
Post by: m0Ray on May 20, 2011, 12:01:46 AM
Look how I solved it here:
https://github.com/m0Ray/bitcoin_account
Requires PHP 5.2 or higher.


Title: Re: PHP JSON sendfrom function
Post by: Alex Beckenham on May 20, 2011, 12:15:10 AM
Thanks m0Ray, I'm taking a look now.

I also updated the 'sendfrom' details on this page:

https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list#Full_list


Title: Re: PHP JSON sendfrom function
Post by: m0Ray on May 20, 2011, 12:31:20 AM
Note that original Sergio Vaccaro's jsonRPCClient class is modified.