php取出json返回null

php中文网
发布: 2016-08-04 09:19:03
原创
1167人浏览过

代码如下:

立即学习PHP免费学习笔记(深入)”;

<code><?php
$url = 'http://music.163.com/api/song/detail/?id='.$_GET['id'].'&ids=%5B'.$_GET['id'].'%5D&csrf_token=';
$json = file_get_contents($url);
$obj = json_decode($json);
$array = array(
    "name" => $obj->songs[0]->bMusic->name,
    "artist" => $obj->songs[0]->artists[0]->name,
    "url" => $obj->songs[0]->mp3Url
);
echo json_encode($array);
?>
</code>
登录后复制

json文件大致是这样:

Find JSON Path Online
Find JSON Path Online

Easily find JSON paths within JSON objects using our intuitive Json Path Finder

Find JSON Path Online 30
查看详情 Find JSON Path Online
<code>{
    "songs": [
        {
            "starred": false,
            "popularity": 95,
            "starredNum": 0,
            "playedNum": 0,
            "dayPlays": 0,
            "hearTime": 0,
            "mp3Url": "http://m2.music.126.net/2b980AHtVx18bJ_Z9MhKXA==/2830142929914115.mp3",
            "rtUrls": [],
            "status": 1,
            "crbt": null,
            "bMusic": {
                "volumeDelta": 0.095421,
                "playTime": 200000,
                "bitrate": 96000,
                "dfsId": 2830142929914115,
                "sr": 44100,
                "name": "Set Fire To The Rain",
                "id": 24667967,
                "size": 2436177,
                "extension": "mp3"
            },
            "rtUrl": null,
            "position": 2,
            "duration": 200000,
            "alias": [],
            "hMusic": {
                "volumeDelta": -0.05,
                "playTime": 200000,
                "bitrate": 320000,
                "dfsId": 2830142929914113,
                "sr": 44100,
                "name": "Set Fire To The Rain",
                "id": 24667965,
                "size": 8056892,
                "extension": "mp3"
            },
            "mMusic": {
                "volumeDelta": 0.41,
                "playTime": 200000,
                "bitrate": 160000,
                "dfsId": 2830142929914114,
                "sr": 44100,
                "name": "Set Fire To The Rain",
                "id": 24667966,
                "size": 4042394,
                "extension": "mp3"
            },
            "lMusic": {
                "volumeDelta": 0.095421,
                "playTime": 200000,
                "bitrate": 96000,
                "dfsId": 2830142929914115,
                "sr": 44100,
                "name": "Set Fire To The Rain",
                "id": 24667967,
                "size": 2436177,
                "extension": "mp3"
            },
            "album": {
                "songs": [],
                "paid": false,
                "onSale": false,
                "status": 1,
                "tags": "",
                "blurPicUrl": "http://p4.music.126.net/J32cgj_Dq-4Wpfs4U9sjwA==/2539871860943087.jpg",
                "companyId": 0,
                "pic": 2539871860943087,
                "description": "",
                "subType": null,
                "alias": [],
                "picId": 2539871860943087,
                "briefDesc": "",
                "artist": {
                    "img1v1Id": 0,
                    "alias": [],
                    "picId": 0,
                    "briefDesc": "",
                    "picUrl": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "albumSize": 0,
                    "img1v1Url": "http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "trans": "",
                    "musicSize": 0,
                    "name": "",
                    "id": 0
                },
                "picUrl": "http://p4.music.126.net/J32cgj_Dq-4Wpfs4U9sjwA==/2539871860943087.jpg",
                "commentThreadId": "R_AL_3_1983765",
                "artists": [
                    {
                        "img1v1Id": 0,
                        "alias": [],
                        "picId": 0,
                        "briefDesc": "",
                        "picUrl": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                        "albumSize": 0,
                        "img1v1Url": "http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                        "trans": "",
                        "musicSize": 0,
                        "name": "Piano Tribute Players",
                        "id": 99094
                    }
                ],
                "copyrightId": 0,
                "publishTime": 1321804800000,
                "company": "CC Entertainment",
                "name": "Piano Tribute to Adele",
                "id": 1983765,
                "type": "专辑",
                "size": 14
            },
            "commentThreadId": "R_SO_4_21515271",
            "artists": [
                {
                    "img1v1Id": 0,
                    "alias": [],
                    "picId": 0,
                    "briefDesc": "",
                    "picUrl": "http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "albumSize": 0,
                    "img1v1Url": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "trans": "",
                    "musicSize": 0,
                    "name": "Piano Tribute Players",
                    "id": 99094
                }
            ],
            "copyrightId": 0,
            "score": 95,
            "fee": 0,
            "mvid": 0,
            "ftype": 0,
            "rtype": 0,
            "rurl": null,
            "copyFrom": "",
            "audition": null,
            "ringtone": "",
            "disc": "",
            "no": 2,
            "name": "Set Fire To The Rain",
            "id": 21515271
        }
    ],
    "equalizers": {},
    "code": 200
}</code>
登录后复制

上传到主机页面输出结果:

Notice: Trying to get property of non-object in on line 6 {"name":null,"artist":"Piano Tribute Players","url":null}

回复内容:

代码如下:

立即学习PHP免费学习笔记(深入)”;

<code><?php
$url = 'http://music.163.com/api/song/detail/?id='.$_GET['id'].'&ids=%5B'.$_GET['id'].'%5D&csrf_token=';
$json = file_get_contents($url);
$obj = json_decode($json);
$array = array(
    "name" => $obj->songs[0]->bMusic->name,
    "artist" => $obj->songs[0]->artists[0]->name,
    "url" => $obj->songs[0]->mp3Url
);
echo json_encode($array);
?>
</code>
登录后复制

json文件大致是这样:

<code>{
    "songs": [
        {
            "starred": false,
            "popularity": 95,
            "starredNum": 0,
            "playedNum": 0,
            "dayPlays": 0,
            "hearTime": 0,
            "mp3Url": "http://m2.music.126.net/2b980AHtVx18bJ_Z9MhKXA==/2830142929914115.mp3",
            "rtUrls": [],
            "status": 1,
            "crbt": null,
            "bMusic": {
                "volumeDelta": 0.095421,
                "playTime": 200000,
                "bitrate": 96000,
                "dfsId": 2830142929914115,
                "sr": 44100,
                "name": "Set Fire To The Rain",
                "id": 24667967,
                "size": 2436177,
                "extension": "mp3"
            },
            "rtUrl": null,
            "position": 2,
            "duration": 200000,
            "alias": [],
            "hMusic": {
                "volumeDelta": -0.05,
                "playTime": 200000,
                "bitrate": 320000,
                "dfsId": 2830142929914113,
                "sr": 44100,
                "name": "Set Fire To The Rain",
                "id": 24667965,
                "size": 8056892,
                "extension": "mp3"
            },
            "mMusic": {
                "volumeDelta": 0.41,
                "playTime": 200000,
                "bitrate": 160000,
                "dfsId": 2830142929914114,
                "sr": 44100,
                "name": "Set Fire To The Rain",
                "id": 24667966,
                "size": 4042394,
                "extension": "mp3"
            },
            "lMusic": {
                "volumeDelta": 0.095421,
                "playTime": 200000,
                "bitrate": 96000,
                "dfsId": 2830142929914115,
                "sr": 44100,
                "name": "Set Fire To The Rain",
                "id": 24667967,
                "size": 2436177,
                "extension": "mp3"
            },
            "album": {
                "songs": [],
                "paid": false,
                "onSale": false,
                "status": 1,
                "tags": "",
                "blurPicUrl": "http://p4.music.126.net/J32cgj_Dq-4Wpfs4U9sjwA==/2539871860943087.jpg",
                "companyId": 0,
                "pic": 2539871860943087,
                "description": "",
                "subType": null,
                "alias": [],
                "picId": 2539871860943087,
                "briefDesc": "",
                "artist": {
                    "img1v1Id": 0,
                    "alias": [],
                    "picId": 0,
                    "briefDesc": "",
                    "picUrl": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "albumSize": 0,
                    "img1v1Url": "http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "trans": "",
                    "musicSize": 0,
                    "name": "",
                    "id": 0
                },
                "picUrl": "http://p4.music.126.net/J32cgj_Dq-4Wpfs4U9sjwA==/2539871860943087.jpg",
                "commentThreadId": "R_AL_3_1983765",
                "artists": [
                    {
                        "img1v1Id": 0,
                        "alias": [],
                        "picId": 0,
                        "briefDesc": "",
                        "picUrl": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                        "albumSize": 0,
                        "img1v1Url": "http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                        "trans": "",
                        "musicSize": 0,
                        "name": "Piano Tribute Players",
                        "id": 99094
                    }
                ],
                "copyrightId": 0,
                "publishTime": 1321804800000,
                "company": "CC Entertainment",
                "name": "Piano Tribute to Adele",
                "id": 1983765,
                "type": "专辑",
                "size": 14
            },
            "commentThreadId": "R_SO_4_21515271",
            "artists": [
                {
                    "img1v1Id": 0,
                    "alias": [],
                    "picId": 0,
                    "briefDesc": "",
                    "picUrl": "http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "albumSize": 0,
                    "img1v1Url": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
                    "trans": "",
                    "musicSize": 0,
                    "name": "Piano Tribute Players",
                    "id": 99094
                }
            ],
            "copyrightId": 0,
            "score": 95,
            "fee": 0,
            "mvid": 0,
            "ftype": 0,
            "rtype": 0,
            "rurl": null,
            "copyFrom": "",
            "audition": null,
            "ringtone": "",
            "disc": "",
            "no": 2,
            "name": "Set Fire To The Rain",
            "id": 21515271
        }
    ],
    "equalizers": {},
    "code": 200
}</code>
登录后复制

上传到主机页面输出结果:

Notice: Trying to get property of non-object in on line 6 {"name":null,"artist":"Piano Tribute Players","url":null}

json还有两个命令:
json_last_error_msg 这个是获取json的错误消息的。
json_last_error 返回最后发生的错误
你可以尝试使用这两个命令来看一下json_encode出现了什么错误。

记得见过PHP官网手册上有个写法类似:

<code>$obj->{$music->name}</code>
登录后复制

这种的,不知道有没有关系……
(类似场景我都是用数组的多些,感觉在php里比对象好用些。。。新手包涵~)

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号