2014-01-15 3 views
0

Oanda.com에서 환율을 받으려고했습니다. 환율이 야후에 비해 실제로 표준이기 때문입니다. Api.문자열을 사용하여 배열로 변경했지만 문자열이 Oanda.com에서 추출한 내용입니다

그래서 지금 나는 어떤 종류의 문자열에있는 세부 사항을 얻을 수 있습니다. 나는 그들을 분리하고 원하는 통화/달러를 원한 정확한 환율을 사용하고 싶었다.

<?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]); 
?> 
+0

[이] (http://stackoverflow.com/questions/11393173/free-real-time-currency-conversion-exchange-rate-xml-feed-url) 문제를 살펴 보자 . – Peon

+0

출력으로 '0.73141'을 원하십니까? –

+0

글쎄, json_decode()를 사용하여 Json 배열을 retiurns 해결하고 그것을 :). 이전에 회신하지 않으셔서 죄송합니다. – Vikram

답변

0
<?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]); 
     } 
} 

} 
    } 
?>