Bitcoin Forum
May 13, 2024, 06:30:27 PM *
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?
1715625027
Hero Member
*
Offline Offline

Posts: 1715625027

View Profile Personal Message (Offline)

Ignore
1715625027
Reply with quote  #2

1715625027
Report to moderator
If you see garbage posts (off-topic, trolling, spam, no point, etc.), use the "report to moderator" links. All reports are investigated, though you will rarely be contacted about your reports.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715625027
Hero Member
*
Offline Offline

Posts: 1715625027

View Profile Personal Message (Offline)

Ignore
1715625027
Reply with quote  #2

1715625027
Report to moderator
1715625027
Hero Member
*
Offline Offline

Posts: 1715625027

View Profile Personal Message (Offline)

Ignore
1715625027
Reply with quote  #2

1715625027
Report to moderator
1715625027
Hero Member
*
Offline Offline

Posts: 1715625027

View Profile Personal Message (Offline)

Ignore
1715625027
Reply with quote  #2

1715625027
Report to moderator
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!