PHP数组cURL foreach
我又遇到了一个问题,我想在ShoutCast2中使用API,但数组有问题PHP数组cURL foreach,php,arrays,curl,Php,Arrays,Curl,我又遇到了一个问题,我想在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
$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 ... />
播放列表名称
打印SimpleXML元素对您没有帮助。其中的数据是类的内部数据 您只需要简单地执行以下操作:
foreach($xml->event as $event){
echo (string)$event['type'];
}
如果你做
var\u dump($event),你会看到什么代码>在这个循环中?我得到一个dumpobject(simplexmlement)#2(5){[“@attributes”]=>array(1){[“type”]=>string(5)“relay”}[“relay”]=>object(simplexmlement){[“@attributes”]=>array(2){[“priority”=>string(1)“1”[“url”=>string(18)“IP:PORT”}[“active”]=>string(1)“1”[“id”]=>string(1)“1”[“calendar”]=>object(simplexmlement){6(1){[@attributes”]=>array(1){[“starttime”]=>string(8)“00:00:00”}}
谢谢您的帮助获得了Hinbecommen:)或者可能没有foreach($xml->data->eventlist->event as$event){echo(string)$event['type'];echo(string)$event['url'];}
输入它输出的url,但不输入,为什么要循环$xml->data->eventlist->event
而不是$xml->event
?另外,根据XML,
元素没有url
属性。它位于
元素上<代码>回显(字符串)$event->relay['url']代码>。