Bitcoin Forum
May 28, 2024, 10:18:30 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: API -> blockchain.info question  (Read 425 times)
AgoraLive (OP)
Full Member
***
Offline Offline

Activity: 221
Merit: 100



View Profile
January 09, 2016, 10:29:42 AM
 #1

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

Activity: 345
Merit: 500



View Profile WWW
January 09, 2016, 09:41:23 PM
 #2

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;
AgoraLive (OP)
Full Member
***
Offline Offline

Activity: 221
Merit: 100



View Profile
January 10, 2016, 04:00:12 PM
 #3

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>";
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!