选择 Curl API 响应的一部分


Selecting part of a Curl API Response

我正在使用 dubtrack.fm API,我对此很陌生,我想这样做。

我正在使用提供的这段代码来调用 API,该 API 让它返回活动歌曲名称:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.dubtrack.fm/room/56aeb4c0e4c463be0a7b9755/playlist/active");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>

我得到这个回复:

{"code":200,"message":"OK","data":{"startTime":211,"song":{"_id":"56b103bc29ac11c701d7a7bb","created":1454441404317,"isActive":true,"isPlayed":false,"skipped":false,"order":999,"roomid":"56aeb4c0e4c463be0a7b9755","songLength":278000,"updubs":0,"downdubs":0,"userid":"560edd4e26d92e0300b09989","songid":"542cbc17cf6f03020041e09d","_user":"560edd4e26d92e0300b09989","_song":"542cbc17cf6f03020041e09d","__v":0,"played":1454441406400},"songInfo":{"name":"Eminem - '"Mom's Spaghetti'" (Music Video)","description":"parody","images":{"thumbnail":"http://i.ytimg.com/vi/SW-BU6keEUw/hqdefault.jpg","youtube":{"sqDefault":"http://i.ytimg.com/vi/SW-BU6keEUw/default.jpg","hqDefault":"http://i.ytimg.com/vi/SW-BU6keEUw/hqdefault.jpg"}},"genre":"Comedy","type":"youtube","fkid":"SW-BU6keEUw","streamUrl":null,"fileUrl":null,"songArtist":"jayssite","songLength":278000,"songBitrate":null,"songMeta":null,"created":1412217879868,"updub":0,"downdub":0,"userid":null,"_id":"542cbc17cf6f03020041e09d","__v":0}}}

我想做的是只打印"名称":"阿姆 - "妈妈的意大利面"(音乐视频)"部分以在网站中使用。我该怎么做?

编辑:

我使用了 JSON 解码,它返回:

object(stdClass)#1 (3) { ["code"]=> int(200) ["message"]=> string(2) "OK" ["data"]=> object(stdClass)#2 (3) { ["startTime"]=> int(123) ["song"]=> object(stdClass)#3 (16) { ["_id"]=> string(24) "56b10b8d99e670ac017811d9" ["created"]=> int(1454443405248) ["isActive"]=> bool(true) ["isPlayed"]=> bool(false) ["skipped"]=> bool(false) ["order"]=> int(999) ["roomid"]=> string(24) "56aeb4c0e4c463be0a7b9755" ["songLength"]=> int(335000) ["updubs"]=> int(0) ["downdubs"]=> int(0) ["userid"]=> string(24) "560edd4e26d92e0300b09989" ["songid"]=> string(24) "56a3ae18fa84db0e008fb1e9" ["_user"]=> string(24) "560edd4e26d92e0300b09989" ["_song"]=> string(24) "56a3ae18fa84db0e008fb1e9" ["__v"]=> int(0) ["played"]=> int(1454443407319) } ["songInfo"]=> object(stdClass)#4 (12) { ["_id"]=> string(24) "56a3ae18fa84db0e008fb1e9" ["name"]=> string(26) "ayy lmao - macarena 5 mins" ["description"]=> string(235) "ayy lmao. ========================== If you Enjoyed then HOORAY!!! =============== Twitter: https://twitter.com/iSniperrzz Instagram: www.instagram.com/craftykoala7_justin Twitch: www.twitch.com/caftykoala7/ Have a good day, Peace!" ["images"]=> object(stdClass)#5 (2) { ["thumbnail"]=> string(48) "https://i.ytimg.com/vi/m6o_uBw3lUU/hqdefault.jpg" ["youtube"]=> object(stdClass)#6 (4) { ["default"]=> object(stdClass)#7 (3) { ["url"]=> string(46) "https://i.ytimg.com/vi/m6o_uBw3lUU/default.jpg" ["width"]=> int(120) ["height"]=> int(90) } ["medium"]=> object(stdClass)#8 (3) { ["url"]=> string(48) "https://i.ytimg.com/vi/m6o_uBw3lUU/mqdefault.jpg" ["width"]=> int(320) ["height"]=> int(180) } ["high"]=> object(stdClass)#9 (3) { ["url"]=> string(48) "https://i.ytimg.com/vi/m6o_uBw3lUU/hqdefault.jpg" ["width"]=> int(480) ["height"]=> int(360) } ["standard"]=> object(stdClass)#10 (3) { ["url"]=> string(48) "https://i.ytimg.com/vi/m6o_uBw3lUU/sddefault.jpg" ["width"]=> int(640) ["height"]=> int(480) } } } ["type"]=> string(7) "youtube" ["fkid"]=> string(11) "m6o_uBw3lUU" ["songLength"]=> int(335000) ["created"]=> int(1453567512726) ["updub"]=> int(0) ["downdub"]=> int(0) ["userid"]=> NULL ["__v"]=> int(0) } } } array(3) { ["code"]=> int(200) ["message"]=> string(2) "OK" ["data"]=> array(3) { ["startTime"]=> int(123) ["song"]=> array(16) { ["_id"]=> string(24) "56b10b8d99e670ac017811d9" ["created"]=> int(1454443405248) ["isActive"]=> bool(true) ["isPlayed"]=> bool(false) ["skipped"]=> bool(false) ["order"]=> int(999) ["roomid"]=> string(24) "56aeb4c0e4c463be0a7b9755" ["songLength"]=> int(335000) ["updubs"]=> int(0) ["downdubs"]=> int(0) ["userid"]=> string(24) "560edd4e26d92e0300b09989" ["songid"]=> string(24) "56a3ae18fa84db0e008fb1e9" ["_user"]=> string(24) "560edd4e26d92e0300b09989" ["_song"]=> string(24) "56a3ae18fa84db0e008fb1e9" ["__v"]=> int(0) ["played"]=> int(1454443407319) } ["songInfo"]=> array(12) { ["_id"]=> string(24) "56a3ae18fa84db0e008fb1e9" ["name"]=> string(26) "ayy lmao - macarena 5 mins" ["description"]=> string(235) "ayy lmao. ========================== If you Enjoyed then HOORAY!!! =============== Twitter: https://twitter.com/iSniperrzz Instagram: www.instagram.com/craftykoala7_justin Twitch: www.twitch.com/caftykoala7/ Have a good day, Peace!" ["images"]=> array(2) { ["thumbnail"]=> string(48) "https://i.ytimg.com/vi/m6o_uBw3lUU/hqdefault.jpg" ["youtube"]=> array(4) { ["default"]=> array(3) { ["url"]=> string(46) "https://i.ytimg.com/vi/m6o_uBw3lUU/default.jpg" ["width"]=> int(120) ["height"]=> int(90) } ["medium"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/m6o_uBw3lUU/mqdefault.jpg" ["width"]=> int(320) ["height"]=> int(180) } ["high"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/m6o_uBw3lUU/hqdefault.jpg" ["width"]=> int(480) ["height"]=> int(360) } ["standard"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/m6o_uBw3lUU/sddefault.jpg" ["width"]=> int(640) ["height"]=> int(480) } } } ["type"]=> string(7) "youtube" ["fkid"]=> string(11) "m6o_uBw3lUU" ["songLength"]=> int(335000) ["created"]=> int(1453567512726) ["updub"]=> int(0) ["downdub"]=> int(0) ["userid"]=> NULL ["__v"]=> int(0) } } } Array

所以我尝试了这段代码:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.dubtrack.fm/room/56aeb4c0e4c463be0a7b9755/playlist/active");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$json = curl_exec($ch);
curl_close($ch);
$obj = json_decode($json);
print $obj->{"name"};
?>

然而它什么也没返回,我仍然不确定如何正确地做到这一点。

看看这里从您给定的代码段来看,您似乎不需要 cUrl。以下示例应该对您有所帮助。

    $url = "https://api.dubtrack.fm/room/56aeb4c0e4c463be0a7b9755/playlist/active";
    $js = file_get_contents($url); //get the JSON from the URL
    $arr = json_decode($js, true) //this function decodes the JSON response into an array
    print $arr['data']['songInfo']['name']; //prints Eminem - "Mom's Spaghetti" (Music Video)