<p>You can also use the <code>filter</code> 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 <code>limit</code> parameter to only fetch one transaction (the latest).</p>

<p>For example <a href="https://blockchain.info/address/1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp?filter=2&amp;limit=1&amp;format=json" rel="nofollow">https://blockchain.info/address/1dice8EMZmqKvrGE4Qc9bUFf9PX3xaYDp?filter=2&amp;limit=1&amp;format=json</a></p>

<p>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 <code>value</code> field (divide by 100,000,000 to get the amount in BTC).</p>

<p>If you are doing this as a way to get notified of incoming payements, I highly recommend you to use the <a href="https://blockchain.info/api/api_receive" rel="nofollow">Receive Payments API</a> as it is dedicated to this use case. </p>

CC BY-SA 3.0
2016-09-25T20:31:08.277Z
2016-09-25T20:31:08.277Z
alcio
<p>Working with JSON data can be a little tricky at first, but you'll get used to it after some practice. </p>

<p>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). </p>

<p>For example to get the most recent transaction you would do:</p>

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

$lastTx = $fgc["txs"][0];
</code></pre>

<p>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. </p>

<pre><code>$lastTx = $fgc["txs"][1];
</code></pre>

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

<p>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 <code>["txs"][0]</code> then we will want to get the outputs <code>["txs"][0]["out"]</code>.  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 <code>if</code> statement to sort it out. </p>

<pre><code>$getOuts = $fgc["txs"][0]["out"];

foreach($getOuts as $outputs){
    $myAddress = $outputs["addr"];
    if($myAddress == $address){
        echo $outputs["value"];
    }
}
</code></pre>

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

<p>Here's a PHPfiddle example: <a href="http://phpfiddle.org/lite/code/rksu-qwq1" rel="nofollow">http://phpfiddle.org/lite/code/rksu-qwq1</a></p>

<p>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: <a href="https://youtu.be/_K270ufQ6ho?t=7m20s" rel="nofollow">https://youtu.be/_K270ufQ6ho?t=7m20s</a> Start at 7:20 mark</p>

CC BY-SA 3.0
2016-09-12T04:29:43.233Z
2016-09-12T04:29:43.233Z
m1xolyd1an
<p>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.</p>

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

<p>For the moment I have as a return of a submitted form:</p>

<pre><code>$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 &gt; 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";
}
</code></pre>

<p>Thank you for your help.</p>

<blockchain.info><php><balance>
With JSON how do I know the amount that was LAST sent to a given address?
dynozor21
# With JSON how do I know the amount that was LAST sent to a given address?

(Asked by: username_0 on 2016-09-11)

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.

**Tags:** blockchain.info, php, balance


---

## 2 answers

### Answer by username_1 on 2016-09-25. Score: 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](https://blockchain.info/api/api_receive) as it is dedicated to this use case.


---

### Answer by username_2 on 2016-09-12. Score: 1

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


---

