WStudio Posted August 19, 2010 Share Posted August 19, 2010 I read the tutorial, "Handling XML Data" and created this script from it. Excellent tutorial by the way Here is my script: <?php // load SimpleXML $fx = new SimpleXMLElement('http://www.boj.org.jm/uploads/fxrates.xml', null, true); echo '<table><tr class="fx_header"><th class="fx_date">'; echo $fx->US[0]->DATE; echo '</th><th class="fx_buy">Buy</th><th class="fx_sell">Sell</th></tr><tr class="fx_us"><td class="fx_legend">USD ($)</td><td class="fx_buy_sell">$'; echo $fx->US[0]->BUY; echo '</td><td class="fx_buy_sell">$'; echo $fx->US[0]->SELL; echo '</td></tr><tr class="fx_cad"><td class="fx_legend">CAD ($)</td><td class="fx_buy_sell">$'; echo $fx->CAD[0]->BUY; echo '</td><td class="fx_buy_sell">$'; echo $fx->CAD[0]->SELL; echo '</td></tr><tr class="fx_gbp"><td class="fx_legend">GBP (£)</td><td class="fx_buy_sell">$'; echo $fx->GBP[0]->BUY; echo '</td><td class="fx_buy_sell">$'; echo $fx->GBP[0]->SELL; echo '</td></tr></table>'; ?> This is the XML file that the script is reading from: http://www.boj.org.jm/uploads/fxrates.xml This is the result page (which is just the way it should look): http://projects.wstudiographics.com/ewl/boj/fxrates.php It works very well, but it take a bit of time to return the result. I think it's because of how the XML file data was set up. The thing is that I have no control over the xml data. I am only allowed to read it. I only need the first or rather latest instance of the data. Any help on this will be greatly appreciated. Thanks in advanced. Winchester (WStudio) Quote Link to comment https://forums.phpfreaks.com/topic/211183-how-can-i-make-this-xml-handling-php-script-faster/ Share on other sites More sharing options...
WStudio Posted August 20, 2010 Author Share Posted August 20, 2010 Is there someone that can help me with? Pretty please? Quote Link to comment https://forums.phpfreaks.com/topic/211183-how-can-i-make-this-xml-handling-php-script-faster/#findComment-1101788 Share on other sites More sharing options...
ram4nd Posted August 20, 2010 Share Posted August 20, 2010 for making it faster you would have to edit your library... or cache the feeds Quote Link to comment https://forums.phpfreaks.com/topic/211183-how-can-i-make-this-xml-handling-php-script-faster/#findComment-1101790 Share on other sites More sharing options...
WStudio Posted August 20, 2010 Author Share Posted August 20, 2010 for making it faster you would have to edit your library... or cache the feeds Hello Ram4nd.. Thank you very much for responding. I'm very new to php. In fact I'm more of a designer and not so much a developer. Could you explain a little about how I should go about this? Could you point me to some scripts that I could learn from? I was thinking, I could find a way to copy the xml data to another xml file on my server and have the script take the info from there. I'm thinking I will need a cron job to have the local xml file update daily. Does this sound like a practical and do-able solution? Quote Link to comment https://forums.phpfreaks.com/topic/211183-how-can-i-make-this-xml-handling-php-script-faster/#findComment-1101831 Share on other sites More sharing options...
kenrbnsn Posted August 21, 2010 Share Posted August 21, 2010 Since you only need the most recent quotes, read those lines into a string and then parse the string. It should be much faster. Try: <?php $i = 0; $fp = fopen('http://www.boj.org.jm/uploads/fxrates.xml','r'); $tmp = ''; while ($i < 16 && !feof($fp)) { $tmp .= fgets($fp, 100); $i++; } $tmp .= "</FX>\n"; fclose($fp); $fx = new SimpleXMLElement($tmp); echo '<table><tr class="fx_header"><th class="fx_date">'; echo $fx->US[0]->DATE; echo '</th><th class="fx_buy">Buy</th><th class="fx_sell">Sell</th></tr><tr class="fx_us"><td class="fx_legend">USD ($)</td><td class="fx_buy_sell">$'; echo $fx->US[0]->BUY; echo '</td><td class="fx_buy_sell">$'; echo $fx->US[0]->SELL; echo '</td></tr><tr class="fx_cad"><td class="fx_legend">CAD ($)</td><td class="fx_buy_sell">$'; echo $fx->CAD[0]->BUY; echo '</td><td class="fx_buy_sell">$'; echo $fx->CAD[0]->SELL; echo '</td></tr><tr class="fx_gbp"><td class="fx_legend">GBP (£)</td><td class="fx_buy_sell">$'; echo $fx->GBP[0]->BUY; echo '</td><td class="fx_buy_sell">$'; echo $fx->GBP[0]->SELL; echo '</td></tr></table>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/211183-how-can-i-make-this-xml-handling-php-script-faster/#findComment-1101949 Share on other sites More sharing options...
MadTechie Posted August 21, 2010 Share Posted August 21, 2010 also as its a remote server you may want to consider caching the file, but it depends on your needs Quote Link to comment https://forums.phpfreaks.com/topic/211183-how-can-i-make-this-xml-handling-php-script-faster/#findComment-1101953 Share on other sites More sharing options...
WStudio Posted August 22, 2010 Author Share Posted August 22, 2010 Hello Ken.. Thank you very much for helping. I will give your codes a try right away Hello MadTechie.. Thanks for your suggestion.. Someone made this suggestion before and I've been searching for pages that will explain how to do this. How would I go about caching the file? Quote Link to comment https://forums.phpfreaks.com/topic/211183-how-can-i-make-this-xml-handling-php-script-faster/#findComment-1102284 Share on other sites More sharing options...
WStudio Posted August 22, 2010 Author Share Posted August 22, 2010 Thank you very much Ken.. the script worked great and by far much faster than what I did before. Quote Link to comment https://forums.phpfreaks.com/topic/211183-how-can-i-make-this-xml-handling-php-script-faster/#findComment-1102289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.