I'm trying to get the value of bitcoin received and the time it was received in the most recent transaction for some bitcoin address using the blockchain.info API. The API displays transaction data in JSON format when requesting a URL like: https://blockchain.info/address/btc_address?format=json.
The response (up until the first transaction) looks like:
{
"hash160": "-",
"address": "-",
"n_tx": 11,
"total_received": 1,
"total_sent": 1,
"final_balance": 1,
"txs": [
{
"ver": 1,
"inputs": [
{
"sequence": 1,
"prev_out": {
"spent": true,
"tx_index": 1,
"type": 0,
"addr": "-",
"value": 1,
"n": 1,
"script": "-"
},
"script": "-"
}
],
"block_height": 1,
"relayed_by": "-",
"out": [
{
"spent": false,
"tx_index": 1,
"type": 0,
"addr": "-",
"value": 1337,
"n": 0,
"script": "-"
},
{
"spent": true,
"tx_index": 1,
"type": 0,
"addr": "-",
"value": 1,
"n": 1,
"script": "-"
}
],
"lock_time": 0,
"result": 0,
"size": 1,
"time": 123456,
"tx_index": 1,
"vin_sz": 1,
"hash": "-",
"vout_sz": 2
},
... more txs ...
]
}
I filtered the data because it's not relevant. The only number I want to retrieve is the value 1337
shown in the first out
of the first txs
and also the time 123456
of the first txs
.
So my PHP looks like this:
$result = json_decode(file_get_contents("https://blockchain.info/address/$bitcoinAddress?format=json"));
$lastPayment = $result->txs[0]->out[0]->value;
$lastPaymentTime = $result->txs[0]->time;
This code works fine but I am getting the notice of Undefined offset: 0
. I don't want to hide PHP notices but why am I even getting it? Can I not reference the JSON array with indexes? Why not and how else do I get the first element in the JSON array? I don't really want to use a loop...
Thanks!
Edit: My editing of the JSON above might cause some confusion... View the full JSON for a BTC address at https://blockchain.info/address/1NTZMU7yKshvWiZYuMquJUHuzusFLZ8SMZ?format=json
Edit 2: Fixed the problem... One of the BTC addresses I was using caused the notices because it hasn't received any bitcoins and so the txs
array was empty...