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

时间:2017-02-14 04:51:32

标签: php json variables icecast

我有一个无线电服务Icecast,我需要获取传输数据,听众,当前歌曲等等。 这些信息是在Json文件中提供给我的:http://213.5.176.74:8002/status-json.xsl 我需要的是创建变量来存储监听器数据,当前歌曲等。 我试着这样做:

grid.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                                Toast.makeText(MainCategoryActivity.this, adapter.getItem(position), Toast.LENGTH_SHORT).show();
                                Intent i = new Intent(MainCategoryActivity.this, ProductNameActivity.class);

                                i.putExtra("MATERIAL_ID", <material_id>);
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(i);
                            }
                        });

当我编写上面的代码时,我得到了这个:

<?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));

问题是我不知道如何使用以前的内容制作变量,所以我在网站上使用它,我将把收音机的统计数据。 我感谢你的回答。 我说西班牙语,我使用谷歌翻译

问候。

1 个答案:

答案 0 :(得分:0)

以你所拥有的为基础......你就近了!

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

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

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'];