Call a API
$jsonurl = "https://api.coindesk.com/v1/bpi/currentprice/USD.json";
$json = file_get_contents($jsonurl);
echo var_dump(json_decode($json));
Which will return this
object(stdClass)[5]
public 'time' =>
object(stdClass)[6]
public 'updated' => string 'May 29, 2016 06:05:00 UTC' (length=25)
public 'updatedISO' => string '2016-05-29T06:05:00+00:00' (length=25)
public 'updateduk' => string 'May 29, 2016 at 07:05 BST' (length=25)
public 'disclaimer' => string 'This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org' (length=155)
public 'bpi' =>
object(stdClass)[7]
public 'USD' =>
object(stdClass)[8]
public 'code' => string 'USD' (length=3)
public 'rate' => string '519.1350' (length=8)
public 'description' => string 'United States Dollar' (length=20)
public 'rate_float' => float 519.135
Or as plain json return
{"time":{"updated":"May 29, 2016 03:59:00 UTC","updatedISO":"2016-05-29T03:59:00+00:00","updateduk":"May 29, 2016 at 04:59 BST"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","bpi":{"USD":{"code":"USD","rate":"516.4050","description":"United States Dollar","rate_float":516.405}}}
Now we want to get data as associative arrays $jsonurl = "https://api.coindesk.com/v1/bpi/currentprice/USD.json";
$json = file_get_contents($jsonurl);
$obj = json_decode($json);
echo $obj->bpi->USD->{'rate'}; // show the rate
No comments:
Post a Comment