Author

Topic: PHP JSON script. calling blockchains.info API (Read 4803 times)

legendary
Activity: 1498
Merit: 1000
October 03, 2012, 09:51:50 AM
#21
hero member
Activity: 812
Merit: 1000
Man is that ugly. I'm going to poke around to I can find a way to output it to nice clean HTML like on blockchain.info

i usually do this instead of var_dump:

echo '
';
print_r($whatever);
echo '
';

makes for an easy visualisation of the variable's structure.
sr. member
Activity: 658
Merit: 250
Quote
object(stdClass)#1 (Cool { ["hash160"]=> string(40) "1f710da1e75db36e0d47eda896a3b50d9e6c9279" ["address"]=> string(34) "13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT" ["n_tx"]=> int(2) ["n_unredeemed"]=> int(0) ["total_received"]=> int(375250000) ["total_sent"]=> int(375250000) ["final_balance"]=> int(0) ["txs"]=> array(2) {
  • => object(stdClass)#2 (12) { ["hash"]=> string(64) "9e7aa7fc7913cfd8ce899472e477451953827ffc9555ffb1bb7b540cadc8b92f" ["ver"]=> int(1) ["vin_sz"]=> int(1) ["vout_sz"]=> int(2) ["size"]=> int(226) ["relayed_by"]=> string(14) "68.118.161.168" ["tx_index"]=> int(19350109) ["result"]=> int(0) ["time"]=> int(1347001988) ["block_height"]=> int(197662) ["inputs"]=> array(1) {
  • => object(stdClass)#3 (1) { ["prev_out"]=> object(stdClass)#4 (5) { ["type"]=> int(0) ["addr"]=> string(34) "13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT" ["value"]=> int(375250000) ["tx_index"]=> int(19347253) ["n"]=> int(0) } } } ["out"]=> array(2) {
  • => object(stdClass)#5 (3) { ["addr"]=> string(33) "1oxCcHwsiREsmBs59nDf88xxjYPLttSkv" ["value"]=> int(275200000) ["type"]=> int(0) } [1]=> object(stdClass)#6 (3) { ["addr"]=> string(34) "1FEps6A6aAqyh4LmnnmuUdtGnfAq5JpgRK" ["value"]=> int(100000000) ["type"]=> int(0) } } } [1]=> object(stdClass)#7 (12) { ["hash"]=> string(64) "68b82640a6a5ed8564a64d22f361fb36f4b226c79b47531253858402b28728bd" ["ver"]=> int(1) ["vin_sz"]=> int(1) ["vout_sz"]=> int(2) ["size"]=> int(227) ["relayed_by"]=> string(14) "122.103.238.28" ["tx_index"]=> int(19347253) ["result"]=> int(-375250000) ["time"]=> int(1346999015) ["block_height"]=> int(197653) ["inputs"]=> array(1) {
  • => object(stdClass)#8 (1) { ["prev_out"]=> object(stdClass)#9 (5) { ["type"]=> int(0) ["addr"]=> string(34) "1MdYdRzAYzcsWHyuS15jdiwH5eEn4BQ6Ck" ["value"]=> int(875300000) ["tx_index"]=> int(19343969) ["n"]=> int(0) } } } ["out"]=> array(2) {
  • => object(stdClass)#10 (3) { ["addr"]=> string(34) "13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT" ["value"]=> int(375250000) ["type"]=> int(0) } [1]=> object(stdClass)#11 (3) { ["addr"]=> string(34) "197AmScUheUZFjVoDPJCXUfEjN6cgZy5s9" ["value"]=> int(500000000) ["type"]=> int(0) } } } } }
Is that what bitcoind gives as output for listaccounts()?

The blockchain.info json rpc api gives me something that looks like:
Quote

Array
(
    [1JzpGDthPGqfsdfeajN5iiVj2SLq4FKpL] => 0.002
    [1sadfweHkPTFL2TogQJz7LbpEsvpQJ1dxfa] => 1.534
    [1MCL86tPrhNCjEsdfwVcrqL7rAsRZZKrrq] => 0.003
)


I guess the blockchain.info api is simplified and higher precision isn't available?
member
Activity: 102
Merit: 10
When I use the blockchain.info listaccounts() call, I get an array with all the bitcoin balances rounded to 3 decimal places.  Is that something I have to handle properly?  How can I get it to go to 8 decimal places?

im pretty sure im getting satoshi, the 3.75250000 yup 8, And a ton of other crap. with the Var_dump

Quote
object(stdClass)#1 (Cool { ["hash160"]=> string(40) "1f710da1e75db36e0d47eda896a3b50d9e6c9279" ["address"]=> string(34) "13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT" ["n_tx"]=> int(2) ["n_unredeemed"]=> int(0) ["total_received"]=> int(375250000) ["total_sent"]=> int(375250000) ["final_balance"]=> int(0) ["txs"]=> array(2) {
  • => object(stdClass)#2 (12) { ["hash"]=> string(64) "9e7aa7fc7913cfd8ce899472e477451953827ffc9555ffb1bb7b540cadc8b92f" ["ver"]=> int(1) ["vin_sz"]=> int(1) ["vout_sz"]=> int(2) ["size"]=> int(226) ["relayed_by"]=> string(14) "68.118.161.168" ["tx_index"]=> int(19350109) ["result"]=> int(0) ["time"]=> int(1347001988) ["block_height"]=> int(197662) ["inputs"]=> array(1) {
  • => object(stdClass)#3 (1) { ["prev_out"]=> object(stdClass)#4 (5) { ["type"]=> int(0) ["addr"]=> string(34) "13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT" ["value"]=> int(375250000) ["tx_index"]=> int(19347253) ["n"]=> int(0) } } } ["out"]=> array(2) {
  • => object(stdClass)#5 (3) { ["addr"]=> string(33) "1oxCcHwsiREsmBs59nDf88xxjYPLttSkv" ["value"]=> int(275200000) ["type"]=> int(0) } [1]=> object(stdClass)#6 (3) { ["addr"]=> string(34) "1FEps6A6aAqyh4LmnnmuUdtGnfAq5JpgRK" ["value"]=> int(100000000) ["type"]=> int(0) } } } [1]=> object(stdClass)#7 (12) { ["hash"]=> string(64) "68b82640a6a5ed8564a64d22f361fb36f4b226c79b47531253858402b28728bd" ["ver"]=> int(1) ["vin_sz"]=> int(1) ["vout_sz"]=> int(2) ["size"]=> int(227) ["relayed_by"]=> string(14) "122.103.238.28" ["tx_index"]=> int(19347253) ["result"]=> int(-375250000) ["time"]=> int(1346999015) ["block_height"]=> int(197653) ["inputs"]=> array(1) {
  • => object(stdClass)#8 (1) { ["prev_out"]=> object(stdClass)#9 (5) { ["type"]=> int(0) ["addr"]=> string(34) "1MdYdRzAYzcsWHyuS15jdiwH5eEn4BQ6Ck" ["value"]=> int(875300000) ["tx_index"]=> int(19343969) ["n"]=> int(0) } } } ["out"]=> array(2) {
  • => object(stdClass)#10 (3) { ["addr"]=> string(34) "13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT" ["value"]=> int(375250000) ["type"]=> int(0) } [1]=> object(stdClass)#11 (3) { ["addr"]=> string(34) "197AmScUheUZFjVoDPJCXUfEjN6cgZy5s9" ["value"]=> int(500000000) ["type"]=> int(0) } } } } }
Man is that ugly. I'm going to poke around to I can find a way to output it to nice clean HTML like on blockchain.info
sr. member
Activity: 658
Merit: 250
When I use the blockchain.info listaccounts() call, I get an array with all the bitcoin balances rounded to 3 decimal places.  Is that something I have to handle properly?  How can I get it to go to 8 decimal places?
hero member
Activity: 812
Merit: 1000
hell yea, getting there. outputted data without formatting 

i usually do this instead of var_dump:

echo '
';
print_r($whatever);
echo '
';

makes for an easy visualisation of the variable's structure.
member
Activity: 102
Merit: 10
your basic code is mal-formed.

try this:

$address = '13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT';   //sets the address
$address_info = get_address_info($address);   //gets the info about the address into $address_info
var_dump($address_info);  //outputs the contents of $address_info to the browser

(and include the get_address_info function too)


hell yea, getting there. outputted data without formatting 
newbie
Activity: 44
Merit: 0
 I wish someone would implement a full client in PHP. Although that's a lot of work of course.
hero member
Activity: 812
Merit: 1000
your basic code is mal-formed.

try this:

$address = '13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT';   //sets the address
$address_info = get_address_info($address);   //gets the info about the address into $address_info
var_dump($address_info);  //outputs the contents of $address_info to the browser

(and include the get_address_info function too)

edit: actually, to start with, use this line in place of line 2 above:
$address_info = get_address_info($address, true);   //gets the info about the address into $address_info
that way, you'll see some handy info on the screen to show what's going on.
member
Activity: 102
Merit: 10
        $address='13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT';
      function get_address_info($address, $debug = false)
        {
                $url = 'http://blockchain.info/address/' . $address . '?format=json';

                if ($debug)
                {
                        echo 'Fetching URL: '.$url.'
';
                }

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                $response = curl_exec($ch);
                curl_close($ch);

                if ($debug)
                {
                        echo '
Curl Response: ';
                        print_r($response);
                        echo '
';
                }

                return json_decode($response);
        }
      ?>

Defined the address
not outputting anything, tinkering
hero member
Activity: 812
Merit: 1000
cURL a thing or that just a typo? what would that php look like?>

it's a thing. although i don't remember what the 'c' stands for.
member
Activity: 102
Merit: 10
ahh, Im not even running bitcoind right now. Im just trying to keep track of an address linked to an offline wallet.

Ill start reading up on bitcoind.

if you're purely just keeping track of an address, you don't even need to use RPC...

you could use cURL to read from blockchain.info's output, and then use PHP's functions to decode that JSON output into an object or array.



awsome, I'm going to try and do that first.

thanks, im adding your code. Ill report back
hero member
Activity: 812
Merit: 1000
here's some code i did using cURL:

Code:
        function get_address_info($address$debug false)
        {
                
$url 'http://blockchain.info/address/' $address '?format=json';

                if (
$debug)
                {
                        echo 
'Fetching URL: '.$url.'
'
;
                }

                
$ch curl_init();
                
curl_setopt($chCURLOPT_URL$url);
                
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
                
curl_setopt($chCURLOPT_TIMEOUT10);
                
$response curl_exec($ch);
                
curl_close($ch);

                if (
$debug)
                {
                        echo 
'
Curl Response: ';
                        
print_r($response);
                        echo 
'
'
;
                }

                return 
json_decode($response);
        }

no jsonRPCClient required.
sr. member
Activity: 476
Merit: 250
Ill start reading up on bitcoind.

It's cool as hell.
hero member
Activity: 812
Merit: 1000
ahh, Im not even running bitcoind right now. Im just trying to keep track of an address linked to an offline wallet.

Ill start reading up on bitcoind.

if you're purely just keeping track of an address, you don't even need to use RPC...

you could use cURL to read from blockchain.info's output, and then use PHP's functions to decode that JSON output into an object or array.

member
Activity: 102
Merit: 10
ahh, Im not even running bitcoind right now. Im just trying to keep track of an address linked to an offline wallet.

Ill start reading up on bitcoind.
hero member
Activity: 812
Merit: 1000
Uploaded the JSON RPC onto the site. Just left it in its folder

Going though the example.php

Should I make a new class say Transactions with this example?

or once I got the jsonrpcphp folder in my root directory i can just call to it?

something like?

$client=new jsonRPCClient('http://blockchain.info/address/$bitcoin_address?format=json');
$txs=$client->gettxs();
 ?>



don't put the blockchain.info address URL in there... put the URL which points to bitcoind.

eg. http://username:[email protected]:8332/ if you're running bitcoind on your own server
or
http://username:[email protected]:80
if you're using blockchain.info's api

you don't need to make a new class.

just call jsonRPCClient with whatever function you need to use.
member
Activity: 102
Merit: 10
Uploaded the JSON RPC onto the site. Just left it in its folder

Going though the example.php

Should I make a new class say Transactions with this example?

or once I got the jsonrpcphp folder in my root directory i can just call to it?

something like?

$client=new jsonRPCClient('http://blockchain.info/address/$bitcoin_address?format=json');
$txs=$client->gettxs();
 ?>

hero member
Activity: 812
Merit: 1000
member
Activity: 102
Merit: 10
I'm new to PHP, looking though a lot scripts on the site and they seem to be doing a lot more then I need. I'm simply trying to get the JSON object for an imputed address and get an array of transactions. And store that data on my site.

Been fooling around with a couple shopping cart's and still trying to grasp what all the JSON-RPC PHP does. I'm thinking it im going a lot farther then I need to do this.

Any pointers or material you could link to me would be greatly Appreciated.
Jump to: