PHP Array cURL foreach -
i have again problem, want use in shoutcast2 api , have array problem.
$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 output:
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 ) ) ) ) ) )
now want read output via array:
foreach ($xml->data->eventlist->event $event ) { echo $event ->type; }
no issue why? error?
thank you
edit original 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 ... />
printing simplexml element not you. data in there internal class.
you want this:
foreach($xml->event $event){ echo (string)$event['type']; }
Comments
Post a Comment