如何从其他url使用Json创建变量PHP

如何从其他url使用Json创建变量PHP,php,json,variables,icecast,Php,Json,Variables,Icecast,我有一个广播服务Icecast,我需要获得传输数据,以及听众、当前歌曲等。 这些信息以Json文件的形式提供给我: 我需要的是创建变量来存储侦听器数据、当前歌曲等。 我试着这样做: <?php $url="http://213.5.176.74:8002/status-json.xsl"; // Initiate curl $ch = curl_init(); // Disable SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYP

我有一个广播服务Icecast,我需要获得传输数据,以及听众、当前歌曲等。 这些信息以Json文件的形式提供给我: 我需要的是创建变量来存储侦听器数据、当前歌曲等。 我试着这样做:

<?php
$url="http://213.5.176.74:8002/status-json.xsl";
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));
问题是,我不知道如何用以前的内容制作变量,所以我在网站上使用它,在那里我会把收音机的统计数据放进去。 我感谢你的回答。 我说西班牙语,我用谷歌翻译


您好。

以您现有的为基础。。。你接近了

您只需要解析返回给您的嵌套数组中的变量

这是相同的数据变量,分开显示嵌套的数组关系:

array(1) { 
    ["icestats"]=> array(7) { 
        ["admin"]=> string(19) "icemaster@localhost" ["host"]=> string(9) "127.0.0.1" ["location"]=> string(5) "Earth" ["server_id"]=> string(13) "Icecast 2.4.3" ["server_start"]=> string(31) "Wed, 08 Feb 2017 19:16:01 +0000" ["server_start_iso8601"]=> string(24) "2017-02-08T19:16:01+0000" 
        ["source"]=> array(13) { 
            ["audio_info"]=> string(10) "bitrate=24" ["genre"]=> string(3) "POP" ["listener_peak"]=> int(1) ["listeners"]=> int(0) ["listenurl"]=> string(28) "http://127.0.0.1:8002/stream" ["server_description"]=> string(6) "(null)" ["server_name"]=> string(6) "AutoDJ" ["server_type"]=> string(10) "audio/mpeg" ["server_url"]=> string(19) "https://habbosk.com" ["stream_start"]=> string(31) "Wed, 08 Feb 2017 19:19:02 +0000" ["stream_start_iso8601"]=> string(24) "2017-02-08T19:19:02+0000" ["title"]=> string(21) "AKONs Lonely Lyrics" ["dummy"]=> NULL 
        } 
    } 
}
这是你的原始代码

<?php
$url="http://213.5.176.74:8002/status-json.xsl";
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

它对我不起作用:(给我这个结果:管理员是:{服务器ID是:{流派是:{歌曲标题是:{@RodneyAraúz抱歉你需要保留你的线路
$result=json_decode($result,true);
见上面的更新谢谢你。效果很好!我已经学会了这一点,所以我没有任何问题了
<?php
$url="http://213.5.176.74:8002/status-json.xsl";
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
$result = json_decode( $result, true ); /* UPDATED */

$stats = $result['icestats'];
echo 'Admin is: ' . $stats['admin'];
echo 'Server ID is: ' . $stats['server_id'];

// and deeper
$source = $stats['source'];
echo 'Genre is: ' . $source['genre'];
echo 'Song Title is: ' . $source['title'];