从PHP中的远程URL读取JSON [英] Reading JSON from remote URL in PHP

查看:83
本文介绍了从PHP中的远程URL读取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个JSON文件:

I have this JSON file:

http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json

并且我正在尝试按照以下方式在PHP中对其进行解码:

and I am trying to decode it in PHP as follows:

$json = file_get_contents('http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json'); 
$data = json_decode($json);
var_dump($data);

但是,我得到的输出是NULL.我想念什么吗?

But, the output I am getting is NULL. Am I missing anything?

推荐答案

使用中的示例PHP.NET json_last_error()我发现您的json语法不正确:

Using example from PHP.NET json_last_error() I found that your json syntax is not correct:

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;
}

输出:

 - Syntax error, malformed JSON

但是,我在以下网站上检查了您的json代码,但显示为有效:

However, I checked your json code in the following website but it says valid:

  • http://www.freeformatter.com/json-validator.html
  • http://jsonlint.com/
  • http://json.parser.online.fr/
  • http://jsonformatter.curiousconcept.com/

这篇关于从PHP中的远程URL读取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆