Jump to content

How can I make this xml handling PHP script faster?


WStudio

Recommended Posts

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 (&#36;)</td><td class="fx_buy_sell">&#36;';
echo $fx->US[0]->BUY;
echo '</td><td class="fx_buy_sell">&#36;';
echo $fx->US[0]->SELL;
echo '</td></tr><tr class="fx_cad"><td class="fx_legend">CAD (&#36;)</td><td class="fx_buy_sell">&#36;';
echo $fx->CAD[0]->BUY;
echo '</td><td class="fx_buy_sell">&#36;';
echo $fx->CAD[0]->SELL;
echo '</td></tr><tr class="fx_gbp"><td class="fx_legend">GBP (&#163;)</td><td class="fx_buy_sell">&#36;';
echo $fx->GBP[0]->BUY;
echo '</td><td class="fx_buy_sell">&#36;';
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)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 (&#36;)</td><td class="fx_buy_sell">&#36;';
echo $fx->US[0]->BUY;
echo '</td><td class="fx_buy_sell">&#36;';
echo $fx->US[0]->SELL;
echo '</td></tr><tr class="fx_cad"><td class="fx_legend">CAD (&#36;)</td><td class="fx_buy_sell">&#36;';
echo $fx->CAD[0]->BUY;
echo '</td><td class="fx_buy_sell">&#36;';
echo $fx->CAD[0]->SELL;
echo '</td></tr><tr class="fx_gbp"><td class="fx_legend">GBP (&#163;)</td><td class="fx_buy_sell">&#36;';
echo $fx->GBP[0]->BUY;
echo '</td><td class="fx_buy_sell">&#36;';
echo $fx->GBP[0]->SELL;
echo '</td></tr></table>';
?>

 

Ken

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.