我又遇到了一个问题,我想在ShoutCast2中使用这个API,并且有一个数组问题。
$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);数组输出:
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
)
)
)
)
)
)现在,我想通过数组读取输出:
foreach ($xml->data->eventlist->event as $event ) {
echo $event ->type;
}为什么没有问题?错误在哪里?
谢谢
编辑原始XML
<eventlist>
<event type="playlist|dj|relay">
<active/>
<id/>
<dj/>
<playlist loopatend="1|0" shuffle="1|0" priority="#">
nameofplaylist
</playlist>
<relay url=""/>
<calendar/>
</event>
<event ... />
发布于 2014-03-24 16:22:00
打印SimpleXML元素无助于您。其中的数据是类的内部数据。
你只想简单地这样做:
foreach($xml->event as $event){
echo (string)$event['type'];
}https://stackoverflow.com/questions/22614656
复制相似问题