【发布时间】:2017-11-25 17:25:08
【问题描述】:
我已经尝试了所有方法,但仍然无法从以下网址获取 JSON 内容:http://onderbrekingen.infrax.be/onderbrekingen/Home/GetOnderbrekingen
这是我最新的 php:
$url = 'http://onderbrekingen.infrax.be/onderbrekingen/Home/GetOnderbrekingen';
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'http://onderbrekingen.infrax.be/onderbrekingen/Home/GetOnderbrekingen');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlSession, $jsonData = json_decode(curl_exec($curlSession));
curl_close($curlSession);
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
echo 'json_decode() - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo 'json_decode() - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo 'json_decode() - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo 'json_decode() - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo 'json_decode() - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo 'json_decode() - Unknown error';
break;
case JSON_ERROR_NONE:
print '<b>JSON var_dump:</b>';
print '</br></br>';
var_dump($jsonData);
print '</br></br><hr></br>';
break;
}
print '<b>echo content:</b>';
print '</br>';
echo stream_get_contents(fopen($url, "r"));
我也用file_get_contents() 试过了,还是不行。
var_dump 每次都返回 NULL。
stream_get_contents 似乎也没有返回原始 JSON 内容。
格兹, 物理化学
【问题讨论】:
-
告诉我们输出而不仅仅是“它不起作用”会很有帮助
-
为什么:curl_setopt($curlSession, $jsonData = json_decode(curl_exec($curlSession));
-
只写:$jsonData = json_decode(curl_exec($curlSession));
标签: php json file-get-contents