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 ... />

1

1 Answer 1

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'];
}
Sign up to request clarification or add additional context in comments.

8 Comments

What do you see if you do var_dump($event); inside this loop?
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" } } }
Thanks got it hinbekommen through your help :)
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
Why are you looping over $xml->data->eventlist->event and not $xml->event? Also, according to your XML, the <event> element doesn't have a url attribute. It's on the <relay> element. echo (string)$event->relay['url'];.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.