【发布时间】:2016-08-25 21:12:00
【问题描述】:
需要:
我需要从 URL 获取 json 数据
问题:
返回空值
试试:
我尝试了 CURL 和 file_get_contents 方法。但都没有给出结果
示例代码:
<?php
ini_set("allow_url_fopen", 1);
$url="http://techpaisa.com/chart/wipro/atr/?xhr";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$r=json_decode($result);
print_r($r);
?>
此代码返回空。我刚试过,复制json并放在我自己的网站上。然后我尝试了,我得到了结果。
我从“http://techpaisa.com/chart/wipro/atr/?xhr”得到以下标题:
Server: nginx/0.7.65
Content-Type: application/json
Keep-Alive: timeout=20
Vary: Accept-Encoding
Transfer-Encoding: chunked
Date: Thu, 25 Aug 2016 20:59:46 GMT
X-Varnish: 462939608 462936037
Age: 3494
Via: 1.1 varnish
Connection: keep-alive
我自己的页面代码和标题:(跳过了一些 Json)
<?php
header('Content-type: application/json');
echo "{'content': 'Date,Price,ATR#1995/02/01,37.0,0.5', 'analysis_type': 'atr', 'text_analysis': 'ATR: 9.57', 'axisname': 'Price', 'analysis_type_verbose': 'Average True Range', 'image_type': 'text/csv', 'symbol': 'wipro', 'fig_title': 'WIPRO Average True Range', 'annotations': '[]', 'set_date_range': true}";
?>
标头值:
Server: Apache
X-Powered-By: PHP/5.3.29
Transfer-Encoding: chunked
Content-Type: application/json
【问题讨论】:
-
这不是有效的 JSON。
-
那么,我必须如何在 php 中显示 json
-
以有效的 JSON 格式 :)