dongqu9917 2017-02-14 04:51
浏览 41
已采纳

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

I have a radio service Icecast, And I need to get the data of the transmission, As are the listeners, Current song, etc. The information is given to me in a Json file: http://213.5.176.74:8002/status-json.xsl What I need is to create variables to store the listeners data, current song, etc. I tried to do this:

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

When I write the code above I get this:

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 } } }

The problem is that I do not know how to make variables with the previous content, so I use it on the website where I will put the statistics of the radio. I appreciate your answers. I speak Spanish, and I use google translate

Greetings.

  • 写回答

1条回答 默认 最新

  • dongxu0690 2017-02-14 05:00
    关注

    Building on what you have... you are close!

    You just need to parse the variable from the nested arrays returned to you.

    Here is your same data variable, separated to show the nested array relationships:

    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 
            } 
        } 
    }
    

    So here is your original code...

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

    And here we process the variable working deeper

    $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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题