Bitcoin Forum

Economy => Web Wallets => Topic started by: AgoraLive on January 09, 2016, 10:29:42 AM



Title: API -> blockchain.info question
Post by: AgoraLive on January 09, 2016, 10:29:42 AM
The following code (modified for your viewing pleasure) "usually" does the trick when I pull latest information I need from the blockchain.info API, but why does this not work? Is it because there are more than 1(one) line which includes a hash?

$json_url3 = "https://blockchain.info/da/unconfirmed-transactions?format=json";
$json3 = file_get_contents($json_url3);
$json3=str_replace('},

]',"}

]",$json3);
$data3 = json_decode($json3);
$latesttx = ($data3->txs->hash);

echo $latesttx;

I have tried to remove the "txs->" but no luck, any errors you see I don't?


Title: Re: API -> blockchain.info question
Post by: micaman on January 09, 2016, 09:41:23 PM
The function 'json_decode' returns an object.
In that object you have an array of txs.
Each of those txs is, again, of type object.
You don't need to replace anything on the '$json3' string.

Code:
$json_url3 = "https://blockchain.info/da/unconfirmed-transactions?format=json";
$json3 = file_get_contents($json_url3);
$data3 = json_decode($json3);

$tx_count = count($data3->txs);
$latesttx = $data3->txs[$tx_count-1]->hash;


Title: Re: API -> blockchain.info question
Post by: AgoraLive on January 10, 2016, 04:00:12 PM
Without replacing and only needing the first character, this code did the trick:

Code:
$json_url3 = "https://blockchain.info/da/unconfirmed-transactions?format=json";
$json3 = file_get_contents($json_url3);

$data3 = json_decode($json3);
$completehash = ($data3->txs[0]->hash);
echo substr($completehash, 0, 1) . "<br>";