1

I have again a problem, I want to use in ShoutCast2 the API and have an array a 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 I want to read the output via array:

foreach ($xml->data->eventlist->event as $event ) {

        echo $event ->type;


    }

no issue is why? where is the 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 ... />

m-eye
  • 23
  • 5

1 Answers1

0

Printing the SimpleXML element will not help you. The data in there is internal to the class.

You just want to simply do this:

foreach($xml->event as $event){
    echo (string)$event['type'];
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • What do you see if you do `var_dump($event);` inside this loop? – gen_Eric Mar 24 '14 at 16:27
  • I get back a dump `object(SimpleXMLElement)#2 (5) { ["@attributes"]=> array(1) { ["type"]=> string(5) "relay" } ["relay"]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(2) { ["priority"]=> string(1) "1" ["url"]=> string(18) "IP:PORT" } } ["active"]=> string(1) "1" ["id"]=> string(1) "1" ["calendar"]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(1) { ["starttime"]=> string(8) "00:00:00" } } }` – m-eye Mar 24 '14 at 16:34
  • Thanks got it hinbekommen through your help :) – m-eye Mar 24 '14 at 16:41
  • Or maybe not ..... `foreach($xml->data->eventlist->event as $event){ echo (string)$event['type']; echo (string)$event['url']; }` type it outputs, the url but not – m-eye Mar 24 '14 at 16:49
  • Why are you looping over `$xml->data->eventlist->event` and not `$xml->event`? Also, according to your XML, the `` element doesn't have a `url` attribute. It's on the `` element. `echo (string)$event->relay['url'];`. – gen_Eric Mar 24 '14 at 16:55
  • because otherwise I get no output – m-eye Mar 24 '14 at 16:58
  • Did `echo (string)$event->relay['url'];` work for you? – gen_Eric Mar 24 '14 at 16:58
  • It's funny ... I get everything now but unfortunately did not read active and id `echo (string)$event->relay['active']; echo (string)$event->relay['id'];` – m-eye Mar 24 '14 at 18:18