如何使用来自其他 url 的 Json 创建变量 PHP
How Create Variable PHP with Json From other url
我有一个广播服务 Icecast,我需要获取传输的数据、听众、当前歌曲等。
信息在 Json 文件中提供给我:http://213.5.176.74:8002/status-json.xsl
我需要的是创建变量来存储听众数据、当前歌曲等。
我尝试这样做:
<?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 } } }
问题是我不知道如何用以前的内容做变量,所以我用它在我将放置广播统计信息的网站上。
感谢您的回答。
我说西班牙语,我使用 google 翻译
你好。
建立在你所拥有的基础上......你很接近!
您只需要解析返回给您的嵌套数组中的变量。
这是相同的数据变量,分开显示嵌套数组关系:
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);
这里我们处理更深层次的变量
$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'];
我有一个广播服务 Icecast,我需要获取传输的数据、听众、当前歌曲等。 信息在 Json 文件中提供给我:http://213.5.176.74:8002/status-json.xsl 我需要的是创建变量来存储听众数据、当前歌曲等。 我尝试这样做:
<?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 } } }
问题是我不知道如何用以前的内容做变量,所以我用它在我将放置广播统计信息的网站上。 感谢您的回答。 我说西班牙语,我使用 google 翻译
你好。
建立在你所拥有的基础上......你很接近!
您只需要解析返回给您的嵌套数组中的变量。
这是相同的数据变量,分开显示嵌套数组关系:
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);
这里我们处理更深层次的变量
$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'];