Page 1 of 1

Newbie - How to parse and display an online xml ?

Posted: Thu Sep 24, 2009 3:15 pm
by micks80
Hi Friends,

I am trying to parse XML with PHP using SimpleXML. But my main concern is that the XML data keeps changing. Secondly, I the XML url is posted online so do you think it's best to read the XML and parse it or it's better to save a local copy and then parse it. So what would be the best way to parse this xml and display the data on my website.

You can see the XML here - http://www.betfair.com/partner/marketDa ... fa=ss&id=4

Thanks
Mick

Re: Newbie - How to parse and display an online xml ?

Posted: Thu Sep 24, 2009 3:27 pm
by requinix
You can't read something on a remote server. The only thing you can do is download a copy and read what you downloaded.

Code: Select all

$simplexml = new SimpleXMLElement("http://www.betfair.com/partner/marketData_loader.asp?fa=ss&id=4", 0, true);
The XML won't change once you've downloaded it.


You should automatically redownload the XML every once in a while. Your code will always work off of that: not from the URL like above.

Code: Select all

$cache = "/path/to/cached/version.xml"; $maxage = 1800; // 30 minutes
// if the file is older than $maxage seconds, download it again
if (filemtime($cache) + $maxage <= time()) copy("http://www.betfair.com/partner/marketData_loader.asp?fa=ss&id=4", $cache);
 
$simplexml = new SimpleXMLElement($cache, 0, true);