Bitcoin Forum
November 07, 2024, 04:28:03 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: PHP JSON script. calling blockchains.info API  (Read 4800 times)
WorldOfBitcoin (OP)
Member
**
Offline Offline

Activity: 102
Merit: 10



View Profile
October 03, 2012, 04:03:15 AM
 #1

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.
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
October 03, 2012, 04:14:57 AM
 #2

this page is also handy: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list
WorldOfBitcoin (OP)
Member
**
Offline Offline

Activity: 102
Merit: 10



View Profile
October 03, 2012, 04:28:19 AM
 #3

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?

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

payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
October 03, 2012, 04:38:06 AM
 #4

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?

<?php
$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:password@127.0.0.1:8332/ if you're running bitcoind on your own server
or
http://username:password@blockchain.info: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.
WorldOfBitcoin (OP)
Member
**
Offline Offline

Activity: 102
Merit: 10



View Profile
October 03, 2012, 04:41:51 AM
 #5

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.
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
October 03, 2012, 04:44:03 AM
 #6

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.

Severian
Sr. Member
****
Offline Offline

Activity: 476
Merit: 250



View Profile
October 03, 2012, 04:44:46 AM
 #7

Ill start reading up on bitcoind.

It's cool as hell.
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
October 03, 2012, 04:46:21 AM
 #8

here's some code i did using cURL:

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

                if (
$debug)
                {
                        echo 
'Fetching URL: '.$url.'<br/>';
                }

                
$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 
'<pre>Curl Response: ';
                        
print_r($response);
                        echo 
'</pre>';
                }

                return 
json_decode($response);
        }

no jsonRPCClient required.
WorldOfBitcoin (OP)
Member
**
Offline Offline

Activity: 102
Merit: 10



View Profile
October 03, 2012, 04:48:05 AM
 #9

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
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
October 03, 2012, 04:48:57 AM
 #10

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.
WorldOfBitcoin (OP)
Member
**
Offline Offline

Activity: 102
Merit: 10



View Profile
October 03, 2012, 04:56:19 AM
 #11

<?php
        $address='13sFP2R1tENbxcxGtH72ceW7YCvFjdLJkT';
      function get_address_info($address, $debug = false)
        {
                $url = 'http://blockchain.info/address/' . $address . '?format=json';

                if ($debug)
                {
                        echo 'Fetching URL: '.$url.'<br/>';
                }

                $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 '<pre>Curl Response: ';
                        print_r($response);
                        echo '</pre>';
                }

                return json_decode($response);
        }
      ?>

Defined the address
not outputting anything, tinkering
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
October 03, 2012, 04:59:28 AM
 #12

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.
bitvientiane
Newbie
*
Offline Offline

Activity: 44
Merit: 0


View Profile
October 03, 2012, 05:01:07 AM
 #13

 I wish someone would implement a full client in PHP. Although that's a lot of work of course.
WorldOfBitcoin (OP)
Member
**
Offline Offline

Activity: 102
Merit: 10



View Profile
October 03, 2012, 05:04:57 AM
 #14

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 
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
October 03, 2012, 05:07:57 AM
 #15

hell yea, getting there. outputted data without formatting 

i usually do this instead of var_dump:

echo '<pre>';
print_r($whatever);
echo '</pre>';

makes for an easy visualisation of the variable's structure.
SuperP
Sr. Member
****
Offline Offline

Activity: 658
Merit: 250



View Profile
October 03, 2012, 06:26:24 AM
 #16

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?
WorldOfBitcoin (OP)
Member
**
Offline Offline

Activity: 102
Merit: 10



View Profile
October 03, 2012, 06:38:36 AM
 #17

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
SuperP
Sr. Member
****
Offline Offline

Activity: 658
Merit: 250



View Profile
October 03, 2012, 07:00:59 AM
 #18

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?
payb.tc
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000



View Profile
October 03, 2012, 12:17:35 PM
 #19

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 '<pre>';
print_r($whatever);
echo '</pre>';

makes for an easy visualisation of the variable's structure.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!