在 PHP 中从远程 URL 读取 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15552385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading JSON from remote URL in PHP
提问by jeewan
I have this JSON file:
我有这个 JSON 文件:
http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json
http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json
and I am trying to decode it in PHP as follows:
我试图用 PHP 解码它,如下所示:
$json = file_get_contents('http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json');
$data = json_decode($json);
var_dump($data);
But, the output I am getting is NULL. Am I missing anything?
但是,我得到的输出是 NULL。我错过了什么吗?
回答by jeewan
Using example from PHP.NET json_last_error()I found that your json syntax is not correct:
使用PHP.NET json_last_error() 中的示例,我发现您的 json 语法不正确:
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
Output:
输出:
- Syntax error, malformed JSON
However, I checked your json code in the following website but it says valid:
但是,我在以下网站中检查了您的 json 代码,但它说有效:
回答by Rawkode
Documentation
文档
json_decodecan return NULLas the documentationstates.
json_decode可以返回NULL如文档所述。
Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
以适当的 PHP 类型返回以 json 编码的值。值 true、false 和 null(不区分大小写)分别返回为 TRUE、FALSE 和 NULL。如果 json 无法解码或编码数据比递归限制更深,则返回 NULL。
Your Problem
你的问题
json_decodeis failing here because of \ninside of id 6
json_decode由于id 6\n内部而失败
{ "id": 6, "url": "http://jeewanaryal.com/angryQuiz/eighties/images/betterOffDead.jpg", "question": "In the movie 'Better Off Dead', what was the \n name of Lane's younger brother?", "answer": [ { "a": { "text": "Bradger", "status": 1 } }, { "b": { "text": "Peter", "status": 0 } }, { "c": { "text": "Frank", "status": 0 } }, { "d": { "text": "Michael", "status": 0 } } ] }
{ "id": 6, "url": " http://jeewanaryal.com/angryQuiz/eighties/images/betterOffDead.jpg", "question": "在电影《Better Off Dead》中,\n莱恩弟弟的名字?", "answer": [ { "a": { "text": "Bradger", "status": 1 } }, { "b": { "text": "Peter", " status": 0 } }, { "c": { "text": "Frank", "status": 0 } }, { "d": { "text": "Michael", "status": 0 } } ] }
Solution
解决方案
I guess your best bet here is to escape them before json_decode
我想你最好的办法是在此之前逃离他们 json_decode
$safe_json = str_replace("\n", "\\n", $json);
$safe_json = str_replace("\n", "\\n", $json);
回答by Christoph Grimmer-Dietrich
Please set the second parameter to true. This will give you an associative array. If the result is still null I'd be interested whether the file_get_contents returned something.
请将第二个参数设置为 true。这将为您提供一个关联数组。如果结果仍然为空,我会对 file_get_contents 是否返回某些内容感兴趣。
回答by jeroen
This is just a guess, but as you do not show what $jsoncontains, I am going to assume that file_get_contentsreturned false, leading to the result you get.
这只是一个猜测,但由于您没有显示$json包含的内容,我将假设file_get_contents返回false,从而导致您得到的结果。
You should check your php.inifile, what is the value for allow_url_fopen? If that is 0that means that you cannot read a file from an httpaddress so that could be the problem.
您应该检查您的php.ini文件,它的值是allow_url_fopen什么?如果那是0意味着您无法从http地址读取文件,那么这可能是问题所在。
Just change the value of allow_url_fopento 1if that is the problem.
如果这是问题,只需更改allow_url_fopento的值1。

