首页
学习
活动
专区
圈层
工具
发布

PHP前端
EN

Stack Overflow用户
提问于 2014-03-24 16:08:47
回答 1查看 1.6K关注 0票数 1

我又遇到了一个问题,我想在ShoutCast2中使用这个API,并且有一个数组问题。

代码语言:javascript
复制
$sc_host = 'IP';
$sc_port = 'PORT';
$sc_user = 'USER';
$sc_pass = 'PASSWORD';
mt_srand((double) microtime() * 1000000);
$seq  = mt_rand(1, 100);
$post = 'op=listevents&seq=' . $seq;
$ch   = curl_init($sc_host . '/api');
curl_setopt($ch, CURLOPT_PORT, $sc_port);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $sc_user . ':' . $sc_pass);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$curl = curl_exec($ch);
$xml  = new SimpleXMLElement(utf8_encode($curl));
curl_close($ch);

数组输出:

代码语言:javascript
复制
Array (
    [@attributes] => Array (
        [seq] => 128
    )
    [data] => Array (
        [eventlist] => Array (
            [event] => Array (
                [@attributes] => Array (
                    [type] => relay
                )
                [relay] => Array (
                    [@attributes] => Array (
                        [priority] => 1
                        [url] => IP:PORT
                    )
                )
                [active] => 1
                [id] => 1
                [calendar] => Array (
                    [@attributes] => Array (
                        [starttime] => 00:00:00
                    )
                )
            )
        )
    )
)

现在,我想通过数组读取输出:

代码语言:javascript
复制
foreach ($xml->data->eventlist->event as $event ) {

        echo $event ->type;


    }

为什么没有问题?错误在哪里?

谢谢

编辑原始XML

代码语言:javascript
复制
<eventlist>
 <event type="playlist|dj|relay">
   <active/>
   <id/>
   <dj/>
   <playlist loopatend="1|0" shuffle="1|0" priority="#">
     nameofplaylist
   </playlist>
   <relay url=""/>
   <calendar/>
 </event>
 <event ... />

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-24 16:22:00

打印SimpleXML元素无助于您。其中的数据是类的内部数据。

你只想简单地这样做:

代码语言:javascript
复制
foreach($xml->event as $event){
    echo (string)$event['type'];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22614656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档