Read and Download a XML File
I was working on a project when i needed to download an XML file and store it localy, to prevent slow scripts, because this XML file had 47 items.
When i would store it my script would work much faster! So i searched, and asked and found this:
<?php $geturl = 'http://www.site.com/example.xml'; $savelocal = 'file.xml'; file_put_contents( $savelocal, file_get_contents( $geturl ) ); ?>
It is this simple and it works totaly fine!
2 Comments »
RSS feed for comments on this post. TrackBack URL
This probably won’t help you make things faster. The difference is that you’re saving it in a file first, and then opening it straight away. So in fact, this is likely a bit slower..
What you should do though is download the file, save it and keep it around for say, 30 minutes. Only when the file is older than 30mins (using filemtime) you download it again. This means that the slowdown only happens for one visitor every 30 minutes.
Alternatively you can avoid saving it on the disk altogether and for example place it in the memory using apc_store / apc_fetch
even i had read your comment i decided to do it after all. And, i saved about half time. So it was an increase for my script. You are talking about reading the xml 15 times, and i think it went from 2 seconds to 1 second.
A XML-playlist of a player (www.1080dots) is the output after the xml has been downloaded. And this playlist is downloaded by the player once every 10 minutes. Everytime the playlist is read, the xml files will be updated. So not that often.
Another web-application is reading the same files localy, but isn’t downloading it from external locations.