doukan4039 2014-01-15 06:45
浏览 21
已采纳

使用字符串更改为数组,但字符串是从Oanda.com提取的详细信息

I was trying to get the currency rate from Oanda.com . because the exchange rate is really standard compared to Yahoo. Api .

So now I am able to get the details which is in some kind of string . I wanted to separate them and use the exact currency rate i want lik USD/CSR .

<?php
    $code=file_get_contents('http://www.oanda.com/embedded/converter/show/b2FuZGFlY2N1c2VyLy9vYW5kYV9ob21lX3BhZ2U=/0/en/');    
    $tmp=explode('<script type="text/javascript">var rates =',$code);
    $tmp=explode('</script>',$tmp[1]);
    $array=trim($tmp[0]);
    var_dump($array);
    $array1 =explode('pairs',$array);
    var_dump($array1[0]);
    var_dump($array1[1]);
?>
  • 写回答

1条回答 默认 最新

  • droxlzcgnr639823 2014-01-31 07:31
    关注
    <?php
    if(!defined("PLUGIN_CONVERSOR_PHP")){
    
    define("PLUGIN_CONVERSOR_PHP",1);
    class plugin_conversor{
        var $parent;/*class parent (plugin.class.php)*/
        var $conversion_rates;
        function plugin_conversor($parent){ /*constructor*/
            $this->parent=$parent;
            $this->conversion_rates=array();
        }
    
    
        public function getYear($pdate) {
            $date_array=explode('-', $pdate);
            return $date_array[0];
        }
    
        public function getMonth($pdate) {
            $date_array=explode('-', $pdate);
            return $date_array[1];
        }
    
        public function getDay($pdate) {
            $date_array=explode('-', $pdate);
            return $date_array[2];
        }
    
        public function createTodayRatesFile(){/*Create the rates file for today*/
            ini_set('display_errors','on'); 
            $info = serialize($this->conversion_rates);
            $path = $this->parent->parent->config->pathPlugins.'conversor/info/'.date('Y').'/'.date('m').'/'.date('d').'/';
            $file = $this->parent->parent->config->geo.'.php';
            if(!file_exists($path))
                mkdir($path, 0777, true);
            file_put_contents($path.$file,$info);   
            ob_start();
            system('chmod 777 '.$file);
            ob_end_clean();
        }
    
        public function checkRatesFile($date){/* Check the file of an specific date */
            ini_set('display_errors','on'); 
            $info=array();
            $path= $this->parent->parent->config->pathPlugins.'conversor/info/'.$this->getYear($date).'/'.$this->getMonth($date).'/'.$this->getDay($date).'/';
            $file=$this->parent->parent->config->geo.'.php';
            if(file_exists($path.$file))
                $info=unserialize(file_get_contents($path.$file));
    
            //print_r($info);
            return $info;
    
        }
    
        public function fetchCur(){ /*Get the conversion rates for all the foreign currencies from oanda*/
            $code=file_get_contents('http://www.oanda.com/embedded/converter/show/b2FuZGFlY2N1c2VyLy9vYW5kYV9ob21lX3BhZ2U=/0/en/');
            $tmp=explode('<script type="text/javascript">var rates =',$code);
            $tmp=explode('</script>',$tmp[1]);
            $array=trim($tmp[0]);
            $array1 = json_decode($array, true);
            $cur = $this->parent->parent->config->conversionCurrencies;
            $rates_array = array();
            $myArray = $array1['pairs'];
            //print_r($myArray);
            $myaskRate = $array1['askRates'];
            $mybidRate = $array1['bidRates'];
            foreach($cur as $key=>$val){
                $cur_key= array_search($val,$myArray); /* get the key for the current currency*/
                $rates_array[$key] = round(($myaskRate[$cur_key]+$mybidRate[$cur_key])/2,2);
            }
    
            $this->conversion_rates = $rates_array;
            $this->createTodayRatesFile();
            echo "<br>"; /*we store in the conversion_rates attribute all the conversion rates for the foreign currencies of the site*/
        }
    
        public function UpdateDB(){
            $this->fetchCur();  
        /*check the conversion rates for yesterday*/
            $yesterday = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
            $yesterday_rates = $this->checkRatesFile($yesterday);
    
            $sql=array();
            $sqlii=array();
            /*pcurrency1 will be used for the price in national currency always. And pcurrency2, 3, etc will be used for the price in the foreign currencies*/
    
            /*First we fill pcurrency1 for all the properties that are in national currency*/
            $sql[]= 'UPDATE propiedades '.
                    'SET Pcurrency1=Palquiler '.
                    ' WHERE Idmoneda='.$this->parent->parent->config->nationalCurrency.' AND Palquiler>0;';
    
            $sql[]= 'UPDATE propiedades '.
                    'SET Pcurrency1=Pventa '.
                    ' WHERE Idmoneda='.$this->parent->parent->config->nationalCurrency.' AND Pventa>0;';
    
            $set_clause=array();
            $search_array = array_diff($yesterday_rates,$this->conversion_rates);
            foreach($this->parent->parent->config->currenciesField as $id_currency=>$field_name){
    
            /*For each foreign currency we fill the value in the corresponding field*/
                if(array_key_exists($id_currency, $search_array))
    
                {
    
                $sql[]= 'UPDATE propiedades '.
                        'SET '.$field_name.'=Palquiler, '.
                        'Pcurrency1=Palquiler*'.$this->conversion_rates[$id_currency].
                        ' WHERE Idmoneda='.$id_currency.' AND Palquiler>0;';
    
                $sql[]= 'UPDATE propiedades '.
                        'SET '.$field_name.'=Pventa, '.
                        'Pcurrency1=Pventa*'.$this->conversion_rates[$id_currency].
                        ' WHERE Idmoneda='.$id_currency.' AND Pventa>0;';   
    
            /*Walk the foreign currencies array of the site*/   
               $sqlii[]='UPDATE propiedades SET '.$field_name.' = Pcurrency1/'.$this->conversion_rates[$id_currency].' WHERE '.$field_name.' IS NULL OR '.$field_name.' <= 0; ';
    
                $set_clause[]=$field_name.' = 0';
            }}  
            /*execute the queries*/
            $query = array_merge($sql,$sqlii);
            $set_string=implode(' , ',$set_clause);
            $sql_query='UPDATE propiedades SET Pcurrency1=0 ';  
            if(count($set_clause)>0)
                $sql_query.=', '.$set_string;
            $sql_query.=';';
    
            echo 'Running... '.$sql_query.'<br>';
            $this->parent->parent->database->query($sql_query);         
            for($i=0; $i<count($query); $i++){
                echo 'Running... '.$query[$i].'<br>';
                $this->parent->parent->database->query($query[$i]);   
            }   
    }
    
    }
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?