使用 JSON 的 Shoutcast 统计数据

Shoutcast stats using JSON

这可能是一个非常简单的答案,但我一直在尝试各种变体以从 shoutcast 数据中提取当前歌曲/艺术​​家。我的代码是:

$str = file_get_contents('https://radio.siteurl.com/api/nowplaying/1/');
$json = json_decode($str, true); // decode the JSON into an associative array
$listeners = $json['listeners'];
$streamer = $json['live']['streamer_name'];

听众和流媒体名称信息都通过了,但我无法为我的生活让当前的歌曲和艺术家通过...

我尝试了以下变体,但这些似乎没有任何效果

  Attempt 1. $current =  $json['live']['song']['now_playing']['text'];
Attempt 2. $current =  $json['song']['text'];
Attempt 3. $current =  $json['live']['now_playing']['text'];
Attempt 4. $current =  $json['now_playing']['text'];

JSON

 {"station":{"id":1,"name":"HabboFests","shortcode":"habbofests","description":"The official and public station!","frontend":"shoutcast2","backend":"liquidsoap","listen_url":"https:\/\/144.91.82.172:8000\/radio.mp3?1576958492","is_public":true,"mounts":[{"path":"\/radio.mp3","is_default":true,"id":21,"name":"128kbps MP3","url":"https:\/\/144.91.82.172:8000\/radio.mp3?1576958492","bitrate":128,"format":"mp3","listeners":{"current":4,"unique":4,"total":4}}],"remotes":[]},"listeners":{"current":4,"unique":4,"total":4},"live":{"is_live":false,"streamer_name":""},"now_playing":{"elapsed":25,"remaining":228,"sh_id":23228,"played_at":1576958475,"duration":253,"playlist":"Christmas Songs","is_request":false,"song":{"id":"e1e0cdfc0bc7b603015f37766222e7a5","text":"Kylie Minogue - Every Day's Like Christmas","artist":"Kylie Minogue","title":"Every Day's Like Christmas","album":"Kylie Christmas","lyrics":"","art":"https:\/\/144.91.82.172\/api\/station\/1\/art\/cb9d8e05cc17cc07400260ef-1575815687.jpg","custom_fields":[]}},"playing_next":{"sh_id":23229,"played_at":0,"duration":159,"playlist":"Christmas Songs","is_request":false,"song":{"id":"60fea47da48da3c68087ffba2e60a13a","text":"The Drifters - White Christmas","artist":"The Drifters","title":"White Christmas","album":"White Christmas \/ The Bells Of St. Mary's [Digital 45]","lyrics":"","art":"https:\/\/144.91.82.172\/api\/station\/1\/art\/50ab3d2ef25ca7968b492457-1575815658.jpg","custom_fields":[]}},"song_history":[{"sh_id":23227,"played_at":1576958303,"duration":175,"playlist":"Christmas Songs","is_request":false,"song":{"id":"63a79704646c11cac1b690d589429c0b","text":"Krystl - Wonderful Time","artist":"Krystl","title":"Wonderful Time","album":"A Lady Christmas","lyrics":"","art":"https:\/\/144.91.82.172\/api\/station\/1\/art\/587ac84c31fd7979a62052b3-1575815576.jpg","custom_fields":[]}},{"sh_id":23226,"played_at":1576958092,"duration":214,"playlist":"Christmas Songs","is_request":false,"song":{"id":"bad0adc241f1fca11e9e95a79c1344f1","text":"Andra Day; Stevie Wonder - Someday at Christmas","artist":"Andra Day; Stevie Wonder","title":"Someday at Christmas","album":"Merry Christmas from Andra Day","lyrics":"","art":"https:\/\/144.91.82.172\/api\/station\/1\/art\/33cf40c7a38fc8743e588c63-1575815650.jpg","custom_fields":[]}},{"sh_id":23225,"played_at":1576957921,"duration":174,"playlist":"Christmas Songs","is_request":false,"song":{"id":"a920bd3f836328ae0612d3a6b480e70a","text":"Miss Montreal - Being Alone At Christmas","artist":"Miss Montreal","title":"Being Alone At Christmas","album":"So... Anything Else?","lyrics":"","art":"https:\/\/144.91.82.172\/api\/station\/1\/art\/1781ab2da39580e64ea25386-1575815579.jpg","custom_fields":[]}},{"sh_id":23224,"played_at":1576957704,"duration":220,"playlist":"Christmas Songs","is_request":false,"song":{"id":"42ba18fb92b572796c3c52b641fd865b","text":"The Darkness - Christmas Time (Don't Let the Bells End)","artist":"The Darkness","title":"Christmas Time (Don't Let the Bells End)","album":"Christmas Time (Don't Let The Bells End)","lyrics":"","art":"https:\/\/144.91.82.172\/api\/station\/1\/art\/d96ea24a8f2d5cfe9dd167d0-1575815721.jpg","custom_fields":[]}},{"sh_id":23223,"played_at":1576957492,"duration":215,"playlist":"Christmas Songs","is_request":false,"song":{"id":"e67e7931ed63555472be551fa284a4b6","text":"Jethro Tull - Ring Out, Solstice Bells","artist":"Jethro Tull","title":"Ring Out, Solstice Bells","album":"Complete Christmas Party","lyrics":"","art":"https:\/\/144.91.82.172\/api\/station\/1\/art\/12103cb786e08dec5a6484aa-1575815644.jpg","custom_fields":[]}}],"cache":"station"}

您需要做的就是按照路径找到您需要的元素,例如:

echo $json['now_playing']['song']['text']; //output: Kylie Minogue - Every Days Like Christmas
echo $json['now_playing']['song']['artist']; //output:Kylie Minogue

您可以使用 print_r()var_dump() 来更好地了解数据路径。