Newbie - How to parse and display an online xml ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
micks80
Forum Newbie
Posts: 1
Joined: Thu Sep 24, 2009 3:09 pm

Newbie - How to parse and display an online xml ?

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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);
Post Reply