2
<?php

$url='http://bart.gov/dev/eta/bart_eta.xml';

$c = curl_init($url);

curl_setopt($c, CURLOPT_MUTE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

$rawXML = curl_exec($c);

curl_close($c);

$fixedupXML = htmlspecialchars($rawXML);

foreach($fixedupXML->eta-> as $eta) {
  echo $eta->destination;
}

?>

As a way to get introduced to PHP, I've decided to parse BART's XML feed and display it on my webpage. I managed (also via this site) to be able to fetch the data and preserve the XML tags. However, when I try to output the XML data, using what I found to be the simplest method, nothing happens.

foreach($fixedupXML->eta as $eta){
  echo $eta->destination;
}

Am I not getting the nested elements right in the foreach loop?

Here is the BART XML feed http://www.bart.gov/dev/eta/bart_eta.xml Thanks!

1
  • What does this mean: $fixedupXML->eta-> ? it seems there is an additional -> or am I missing something? Commented Jan 25, 2012 at 0:51

1 Answer 1

7

You may want to look at simplexml, which is a fantastic and really simple way to work with XML. Here's a great example:

$xml = simplexml_load_file('http://bart.gov/dev/eta/bart_eta.xml');

Then you can run a print_r on $xml to see it's contents:

print_r($xml);

And you should be able to work with it from there :)

If you still need to use curl to get the feed data for some reason, you can feed the XML into simplexml like this:

$xml = simplexml_load_string($rawXML);
Sign up to request clarification or add additional context in comments.

Comments

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.