javascript – Problem with parsing xml file using php. Url address doesn’t contain .xml extension – Education Career Blog

Hey guys. I’m trying to parse xml file in order to extract some data from it and display it on the website. However, when I’m using url which doesn’t end with .xml extension I can’t run a foreach loop on the result. In other words when I’m using this kind of url:

$url =

I can run a a foreach loop on it and it works like charm. But this time around I have to deal with this type of url:

$url =

So you can clearly see that it doesn’t contain .xml extension. It returns nicely parsed result but I can’t get the foreach loop to work on this result and therefore I can’t extract any data from it. Could you help me with this one?

Example of what I’m doing:

$url = '';

    $sx = simplexml_load_file($url);

    foreach ($sx->response as $row)
        echo $row'status';


And this is the result of var_dump($sx):

object(SimpleXMLElement)#334 (2) { "@attributes"=>  array(2) { "status"=>  string(2) "ok" "version"=>  string(3) "1.0" } "events"=>  object(SimpleXMLElement)#333 (2) { "@attributes"=>  array(3) { "resultCount"=>  string(1) "1" "pageSize"=>  string(2) "15" "pageIndex"=>  string(1) "0" } "event"=>  object(SimpleXMLElement)#338 (10) { "@attributes"=>  array(1) { "id"=>  string(6) "180085" } "name"=>  string(27) "Lady Gaga Monster Ball Tour" "doordatetime"=>  string(19) "2010-11-01T18:30:00" "tickets"=>  object(SimpleXMLElement)#337 (1) { "ticket"=>  array(5) { 0=>  object(SimpleXMLElement)#339 (7) { "@attributes"=>  array(1) { "id"=>  string(6) "307902" } "price"=>  string(7) "£55.00" "url"=>  string(32) "" "provider"=>  string(12) "Ticketmaster" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(5) "false" } 1=>  object(SimpleXMLElement)#340 (7) { "@attributes"=>  array(1) { "id"=>  string(6) "426248" } "price"=>  string(7) "£82.00" "url"=>  string(32) "" "provider"=>  string(8) "Seatwave" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(4) "true" } 2=>  object(SimpleXMLElement)#437 (6) { "@attributes"=>  array(1) { "id"=>  string(6) "306230" } "url"=>  string(32) "" "provider"=>  string(7) "Viagogo" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(4) "true" } 3=>  object(SimpleXMLElement)#438 (6) { "@attributes"=>  array(1) { "id"=>  string(6) "325819" } "url"=>  string(32) "" "provider"=>  string(15) "WorldTicketShop" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(4) "true" } 4=>  object(SimpleXMLElement)#342 (6) { "@attributes"=>  array(1) { "id"=>  string(6) "402593" } "url"=>  string(32) "" "provider"=>  string(14) "Empire Tickets" "soldout"=>  string(5) "false" "ticketsavailable"=>  string(4) "true" "issecondary"=>  string(4) "true" } } } "venue"=>  object(SimpleXMLElement)#336 (9) { "@attributes"=>  array(1) { "id"=>  string(4) "1479" } "name"=>  string(13) "Odyssey Arena" "street"=>  string(13) "2 Queens Quay" "town"=>  string(7) "Belfast" "nameandtown"=>  string(22) "Odyssey Arena, Belfast" "country"=>  string(2) "GB" "gjurl"=>  string(90) "" "latitude"=>  string(9) "54.602158" "longitude"=>  string(9) "-5.918215" } "gjurl"=>  string(90) "" "artists"=>  object(SimpleXMLElement)#341 (1) { "artist"=>  object(SimpleXMLElement)#332 (5) { "@attributes"=>  array(2) { "id"=>  string(5) "54194" "isprimary"=>  string(4) "true" } "name"=>  string(9) "Lady Gaga" "gjurl"=>  string(42) "" "thumbnailimage"=>  string(68) "" "mediumimage"=>  string(68) "" } } "iscancelled"=>  string(5) "false" "genre"=>  string(8) "Rock/Pop" "image"=>  string(68) "" } } } 


Just use the simplexml_load_file function, it will return False if can’t parse the xml

$url = ''
    // is a xml
else {
    // no xml found

You can find more info here:


Your processing doesn’t seem to match with your XML document structure. Your processing seems to be expecting a document like this:

  <response status="ok">...</response>
  <response status="ok">...</response>

However, by the looks of your var_dump, that’s not how the actual document is structured. That’s why you can’t iterate over the “response” elements with foreach() — I don’t think there are any there to iterate over.

If you can post the actual XML document, we can probably fix up your processing to match it.

The main thing to bear in mind, I think, is that the way SimpleXMLElement works is that the object you get back is the root of the XML document, so if your document looks like this:


…then $sx->response won’t exist. $sx is the <response> element, it doesn’t contain it.

Leave a Comment