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条)

报告相同问题?

悬赏问题

  • ¥15 recipe通过gem协议传的是什么
  • ¥15 TS2307: Cannot find module 'cc'.
  • ¥15 100小时学会sap 书上pp章节5.22,标准成本计算逻辑?
  • ¥50 达梦数据库误删日志文件重做DAMENG01.log启动仍然-712错误
  • ¥15 cellranger化学处理类型报错
  • ¥15 用texstudio插入图片出现下面情况,怎么办
  • ¥15 ubantu 用samba挂载windows的共享文件夹,无法挂载二级目录和修改文件
  • ¥15 有没有会五轴RTCP算法,双转台AC结构。
  • ¥25 对于LSTM实践问题的疑问
  • ¥15 PHP中关于排名和显示的问题