JSON解析永远消失并提供错误信息

时间:2016-12-22 03:06:20

标签: php json

我正在尝试使用我在网上找到的某人的JSON解析脚本来阅读icecast信息,但加载速度非常慢并且向我提供了错误的信息。

我希望它能够获得这种类型,但是当有DJ时它会一直显示为自动DJ。

PHP脚本:

<?php
$url = "http://94.23.146.93:8124/status-json.xsl"; 

$ch = curl_init(); 

curl_setopt( $ch, CURLOPT_URL, $url ); 
curl_setopt( $ch, CURLOPT_HEADER, false ); 
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Accept-Encoding: gzip, deflate, sdch' ) ); 
curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] ); 
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); 
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); 
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 ); 
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); 

$result = json_decode( curl_exec( $ch ) ); 

curl_close($ch); 

$genre = "Auto DJ"; 

if(isset($result->icestats->source[1]->genre)) { 
    $genre = $result->icestats->source[1]->genre; 
} 

echo $genre;
?>

.XSL文件:

{"icestats":{"admin":"icemaster@localhost","host":"94.23.146.93","location":"Earth","server_id":"Icecast 2.4.2","server_start":"Wed, 21 Dec 2016 18:54:07 -0500","server_start_iso8601":"2016-12-21T18:54:07-0500","source":[{"audio_info":"bitrate=320;samplerate=44100;channels=2","bitrate":320,"channels":2,"genre":"Aria.","listener_peak":3,"listeners":0,"listenurl":"http://94.23.146.93:8124/autodj","samplerate":44100,"server_description":"Unspecified description","server_name":"Frank","server_type":"audio/mpeg","server_url":"http://localhost/","stream_start":"Wed, 21 Dec 2016 18:54:08 -0500","stream_start_iso8601":"2016-12-21T18:54:08-0500","title":"Clean Bandit - Rockabye (feat. Sean Paul & Anne-Marie)","dummy":null},{"listeners":0,"listenurl":"http://94.23.146.93:8124/live","dummy":null},{"listeners":0,"listenurl":"http://94.23.146.93:8124/stream","dummy":null}]}}

1 个答案:

答案 0 :(得分:0)

您的结果是这样的:

stdClass Object
(
    [icestats] => stdClass Object
        (
            [admin] => icemaster@localhost
            [host] => 94.23.146.93
            [location] => Earth
            [server_id] => Icecast 2.4.2
            [server_start] => Wed, 21 Dec 2016 18:54:07 -0500
            [server_start_iso8601] => 2016-12-21T18:54:07-0500
            [source] => Array
                (
                    [0] => stdClass Object
                        (
                            [audio_info] => bitrate=320;samplerate=44100;channels=2
                            [bitrate] => 320
                            [channels] => 2
                            [genre] => Aria.
                            [listener_peak] => 3
                            [listeners] => 0
                            [listenurl] => http://94.23.146.93:8124/autodj
                            [samplerate] => 44100
                            [server_description] => Unspecified description
                            [server_name] => Frank
                            [server_type] => audio/mpeg
                            [server_url] => http://localhost/
                            [stream_start] => Wed, 21 Dec 2016 18:54:08 -0500
                            [stream_start_iso8601] => 2016-12-21T18:54:08-0500
                            [title] => Little Mix - Shout Out To My Ex
                            [dummy] => 
                        )

                    [1] => stdClass Object
                        (
                            [listeners] => 0
                            [listenurl] => http://94.23.146.93:8124/live
                            [dummy] => 
                        )

                    [2] => stdClass Object
                        (
                            [listeners] => 0
                            [listenurl] => http://94.23.146.93:8124/stream
                            [dummy] => 
                        )

                )

        )

)

类型仅出现在$result->icestats->source[0]->genre

我建议你使用

$result = json_decode(file_get_contents($url));

而不是大卷曲片段。

我希望这会对你有所帮助。