donglizhan7848 2016-12-27 18:30
浏览 56
已采纳

无法通过网址从URL解码json

I am trying to read json vi php but unable to decode

<?php 
$json_url = "http://chartapi.finance.yahoo.com/instrument/1.0/AAPL/chartdata;type=quote;range=5d/json";
$json = file_get_contents($json_url);

$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";

?>
  • 写回答

3条回答 默认 最新

  • douduan5753 2016-12-27 18:45
    关注

    If you visit your target URL you will notice the JSON value is wrapped in a callback method which you could trim as by something like

    $json = str_replace('finance_charts_json_callback(', '', substr($pageContent, 0, strlen($pageContent) - 1));
    

    Here is how it looks all put together:

    <?php 
    $json_url = "http://chartapi.finance.yahoo.com/instrument/1.0/AAPL/chartdata;type=quote;range=5d/json"; $pageContent = file_get_contents($json_url);
    
    $json = str_replace('finance_charts_json_callback(', '', substr($pageContent, 0, strlen($pageContent) - 1));
    
    $data = json_decode($json, TRUE);
    
    echo "<pre>"; print_r($data); echo "</pre>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?