php货币减慢页面

时间:2014-10-24 08:14:59

标签: php currency

我有一个页面粉丝广告,我在" RON"中设置了页面货币。我转换为显示在"欧元"但在循环中是非常慢..我试图包括脚本形式其他PHP但stil相同...我尝试了很多货币兑换器,但都有相同的问题..慢下来的页面...如果我把代码直接进入循环然后告诉我一个错误:该类无法重复。 这是我使用的php货币:

<?php 
class cursBnrXML
 {

     var $currency = array();

    function cursBnrXML($url)
    {
        $this->xmlDocument = file_get_contents($url);
        $this->parseXMLDocument();
    }


    function parseXMLDocument()
    {
         $xml = new SimpleXMLElement($this->xmlDocument);

         $this->date=$xml->Header->PublishingDate;

         foreach($xml->Body->Cube->Rate as $line)    
         {                      
             $this->currency[]=array("name"=>$line["currency"], "value"=>$line, "multiplier"=>$line["multiplier"]);
         }
    }
    function getCurs($currency)
    {
        foreach($this->currency as $line)
        {
            if($line["name"]==$currency)
            {
                return $line["value"];
            }
        }

        return "Incorrect currency!";
    }
 }
//@an example of using the cursBnrXML class
$curs=new cursBnrXML("http://www.bnr.ro/nbrfxrates.xml");
 ?>

1 个答案:

答案 0 :(得分:0)

您可以将cursBnrXML类修改为缓存解析后的货币,这样您就不必在每次查找时再次遍历整个集合。

<?php 
class cursBnrXML
 {

    private $_currency = array();

    # keep the constructor the same


    # modify this method
    function parseXMLDocument()
    {
         $xml = new SimpleXMLElement($this->xmlDocument);

         $this->date=$xml->Header->PublishingDate;

         foreach($xml->Body->Cube->Rate as $line)    
         {                      
             $this->currency[$line["currency"]]=array(
                "value"=>$line, 
                "multiplier"=>$line["multiplier"]
             );
         }
    }

    # modify this method too
    function getCurs($currency)
    {
        if (isset($this->_currency[$currency])) 
        {
             return $this->_currency[$currency]['value'];
        }

        return "Incorrect currency!";
    }
 }
//@an example of using the cursBnrXML class
$curs=new cursBnrXML("http://www.bnr.ro/nbrfxrates.xml");
 ?>