2

The API is:

{"status":1,"data":[{"address":"0x5c48aebbbbdcf44f5f181edbb5b20a68210eacfe","balance":6000000000000000000,"nonce":null,"code":"0x","name":null,"storage":null,"firstSeen":null}]}

I am trying to get the value from Balance (6000000000000000000). I also need to divide this value with 1000000000000000000 in meantime...

I tried:

$obj = json_decode($json,true);
$address = $obj['address'];

$json = file_get_html('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
$v = (get_object_vars($obj->data));
$balance = $v['balance'];
$_SESSION['balance'] = $balance;

It doesn't work.

The same code works with the API below:

{"status":"success","data":{"address":"LU4P8fVNj8xv2dwRc7fdXmpaW2xuCSJvUK","is_unknown":false,"balance":0,"balance_multisig":0,"totalreceived":0.0340576,"nb_txs":11,"first_tx":{"time_utc":"2015-08-30T09:51:05Z","tx":"10f4b55d0bead8d3d84fe27433db20b63368d65bc043c651d59dbe1342d14098","block_nb":"842330","value":0.00704229,"confirmations":251241},"last_tx":{"time_utc":"2016-02-02T16:09:23Z","tx":"c666d3acf6f57fd86a2ccc9537ee022167da408f26193e4abbd7b8148fc518b3","block_nb":"932771","value":-0.01250452,"confirmations":160800},"is_valid":true},"code":200,"message":""}

What am I doing wrong?

Jim J
  • 23
  • 2
  • The data is located in `$obj['data']['address']` perhaps next time do a little bit of debugging first with `var_dump()` – Xorifelse Nov 08 '16 at 02:34
  • @Xorifelse should it be `$v = (get_object_vars($obj->data->address));` `$balance = $v['balance'];` i still can't get it to work. sorry a newbie here. – Jim J Nov 08 '16 at 02:39
  • The url that is constructed: https://etherchain.org/api/account/LU4P8fVNj8xv2dwRc7fdXmpaW2xuCSJvUK, returns the data as empty set: {"status":1,"data":[]}, balance wouldn't be in it and php and will cause php to bark a warning that balance is not part of object (check warning level and php log), unless that url behaves differently if logged in on your machine – danchik Nov 08 '16 at 02:44
  • Yeah, I missed the part where its also the first array. Updated it in my answer. – Xorifelse Nov 08 '16 at 02:47
  • The exact url is: https://etherchain.org/api/account/0x5c48aebbbbdcf44f5f181edbb5b20a68210eacfe – Jim J Nov 08 '16 at 02:50
  • oh then it is just `$v[0]['balance'];` – danchik Nov 08 '16 at 02:50
  • @danchik Now you're missing the part where it is also in the data array.. – Xorifelse Nov 08 '16 at 02:51
  • @JimJ Just look at my answer, works. – Xorifelse Nov 08 '16 at 02:52
  • @Xorifelse he edited the code to already parse the data into $v: `$v = (get_object_vars($obj->data));` – danchik Nov 08 '16 at 02:52
  • @danchik My original code works with the 2nd API but not with the 1st one. and the two APIs are almost the same. There gotta be some small edit which needs to be done (and which I am not seeing).. – Jim J Nov 08 '16 at 03:17

1 Answers1

1

Its because you're missing that the address is located in the first data array in the JSON string and in the second JSON string, no array exists inside the data array.

$obj = json_decode($json,true);
$address = $obj['data'][0]['address'];
$json = file_get_contents('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
echo $obj->data[0]->balance;
Xorifelse
  • 7,878
  • 1
  • 27
  • 38
  • I tried: `$obj = json_decode($json,true);` `$address = $obj['data'][0]['address'];` `$json = file_get_html('https://etherchain.org/api/account/'.$address); $obj = json_decode($json); $v = (get_object_vars($obj->data[0]->balance));` `$balance = $v['balance']; $_SESSION['balance'] = $balance;` And still nothing. – Jim J Nov 08 '16 at 02:56
  • I really don't understand what you're trying to do with `get_object_vars()`, why not convert it to an array directly? Anyways, updated the code. – Xorifelse Nov 08 '16 at 03:02
  • @JimJ The second JSON string `"data":{` does not contain an array. The first JSON string does: `"data":[{"ad` hence the extra required `[0]` – Xorifelse Nov 08 '16 at 03:25