从json API

时间:2017-02-07 00:00:12

标签: php json api

我试图从此API中提取突出显示的变量:

{  
   "timestamp":{  
      "total":1486424886,
      "exchanges":{  
         "NEG":1486423855,
         "MBT":1486424738,
         "LOC":1486422237,
         "FOX":1486424483,
         "FLW":1486411044,
         "B2U":1486424811,
         "ARN":1486405596
      }
   },
   "ticker_24h":{  
      "total":{  
         "last":**3011.8756088755**, // <---
         "high":4073.32,
         "low":2631.58,
         ...

http://api.bitvalor.com/v1/ticker.json

到目前为止,这是我的代码:

<html>
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<table width="auto">
<tr><td>BTC/BRL (Bitvalor)</tr></td>
<tr><td>

<?php
$url1 = "http://api.bitvalor.com/v1/order_book_stats.json";
$fgc1 = file_get_contents($url1);
$json1 = json_decode($fgc1, true);
$price1 = $json1["ticker_24h.total.last"];
echo $price1;
?>

</tr></td>
</table>
</html>

我错过了什么?

1 个答案:

答案 0 :(得分:2)

您可以像关联数组一样访问已解码的JSON:

$price1 = $json1['ticker_24h']['total']['last'];

如果数据格式发生变化或响应不符合预期,请务必使用isset