I am trying to build a script that will give me the amount a user has sent to a given bitcoin address. Currently this data is in JSON format.

How can I use this JSON data to know the amount that was LAST sent to a given address?

For the moment I have as a return of a submitted form:

$userIDsent = $_POST['userID'];
$adrsBTCsent = $_POST['adrsBTC'];

$bcInfo = json_decode(file_get_contents("https://blockchain.info/address/". $adrsBTCsent ."?format=json"), true);

$nombre_de_transac  = $bcInfo["n_tx"];
$valeur_en_satochi = $bcInfo["total_received"];

If ($nombre_de_transac > 1){
    /* more then 1 transaction for that BTC adds. I need to know what is the amount last sent and NOT the total amount used on that address */

} else {
    /* 1 transaction this address is new so let's get the amount of BTC sent */
    echo "The user ".$userIDsent." has sent ". $valeur_en_satochi . "uBTC";
    echo "bra, bla, bla lets continue";

Thank you for your help.


2 Answers 2


You can also use the filter GET parameter to ensure that the first transaction is one where the address receives bitcoins and not one where the address spent bitcoins and the limit parameter to only fetch one transaction (the latest).

For example https://blockchain.info/address/1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp?filter=2&limit=1&format=json

To get the amount sent, you'll have to filter outputs to get the one sending BTC to the address you're interested in and then get the value field (divide by 100,000,000 to get the amount in BTC).

If you are doing this as a way to get notified of incoming payements, I highly recommend you to use the Receive Payments API as it is dedicated to this use case.


Working with JSON data can be a little tricky at first, but you'll get used to it after some practice.

To fetch blockchain.info's JSON data in PHP, as you have in your example, you just need to pull the JSON data into an array in PHP and the cycle through the array(s).

For example to get the most recent transaction you would do:

$address = "1NPrfWgJfkANmd1jt88A141PjhiarT8d9U";
$link = "https://blockchain.info/address/".$address."?format=json";
$fgc = json_decode(file_get_contents($link), true);

$lastTx = $fgc["txs"][0];

The zero means we want the first, or most recent transaction. If you wanted the 2nd most recent transaction you would just change that to a 1.

$lastTx = $fgc["txs"][1];

Now $lastTx will hold an array of all the transaction data for that specific transaction including inputs and outputs.

Your question is how to get the amount of the most recent transaction to an address. So first we will get the most recent transaction ["txs"][0] then we will want to get the outputs ["txs"][0]["out"]. The trick here is that many times there are more than one outputs for a transaction and they don't always correspond to the same address you are querying. So we'll need to create a loop and a simple if statement to sort it out.

$getOuts = $fgc["txs"][0]["out"];

foreach($getOuts as $outputs){
    $myAddress = $outputs["addr"];
    if($myAddress == $address){
        echo $outputs["value"];

This will output the most recent value in satoshis. If you want it in full bitcoins, just divide it by 100 million.

Here's a PHPfiddle example: http://phpfiddle.org/lite/code/rksu-qwq1

If you're looking to learn more about how to read and handle JSON data and parse it with PHP here's a video tutorial: https://youtu.be/_K270ufQ6ho?t=7m20s Start at 7:20 mark


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.