2014-11-23 64 views
0

我正嘗試使用Google API和PHP curl轉換貨幣數據。不幸的是我有以下問題,我仍然無法解決它。任何人都可以幫助我。 這裏是我的PHP函數。使用Google和CURL進行貨幣轉換php

function currency($from_Currency,$to_Currency,$amount) { 
    $amount = urlencode($amount); 
    $from_Currency = urlencode($from_Currency); 
    $to_Currency = urlencode($to_Currency); 
    $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency"; 
    $ch = curl_init(); 
    $timeout = 0; 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    $rawdata = curl_exec($ch); 
    curl_close($ch); 
    $data = explode('"', $rawdata); 
    $data = explode(' ', $data['3']); 
    $var = $data['0']; 
    return round($var,2); 
} 

//這裏是我的函數調用

$usd= currency("USD","ETB",1); 
echo "270 USD= ".$usd."ETB"; 

,但我遇到的follwing錯誤

注意:未定義抵消:3 ...上線...(14此功能)

+0

'var_dump($ rawdata)'看看你實際得到了什麼。 – Passerby 2014-11-23 07:33:44

+0

Igoogle已關閉並且您的網址結構也不正確 – 2014-11-23 07:36:44

+0

@ Passerby,它會重蹈覆轍!這是什麼意思 – Mehari 2014-11-23 07:41:51

回答

1

看來你的API URL是錯誤的,因爲當$rawdata回聲版,谷歌返回錯誤404。就目前而言,可用的解決方案(如果你堅持使用谷歌)是轉換使用Google財經的貨幣。例如,使用GET要求,你可以用下面的格式發送一個請求:

$url = "https://www.google.com/finance/converter?a=" . $amount . "&from=" . $from . "&to=" . $to; 

不幸的是,谷歌返回一個完整的網頁HTML,所以你需要手工分析結果。剛纔我試圖用https://www.google.com/finance/converter?a=1&from=IDR&to=USD訪問它,並且將轉換結果在<div>這樣附:

<div id=currency_converter_result>1 IDR = <span class=bld>0.0001 USD</span> 

因此,如果您將結果保存在$rawdata變量,你可以在PHP中使用正則表達式以獲得轉換結果。由於它不是一個正式的API,所以如果代碼不起作用,你需要在下次積極觀察頁面結構。

這是你的代碼,以測試更新:

function convertCurrency($amount, $from_Currency, $to_Currency) { 
    $url = "https://www.google.com/finance/converter?a=" . $amount . "&from=" . $from_Currency . "&to=" . $to_Currency; 
    $ch = curl_init(); 
    $timeout = 0; 

    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    $rawdata = curl_exec($ch); 

    curl_close($ch); 

    preg_match("/<span class=bld>(.*)<\/span>/", $rawdata, $converted); 
    $converted = preg_replace("/[^0-9.]/", "", $converted); 

    return round($converted[0], 3); 
} 

$usd = currency("USD", "ETB", 270); 
echo "270 USD = " . $usd . " ETB"; 

我的建議是:找到貨幣轉換的另一API。您可以尋找替代品,如https://openexchangerates.org/。不幸的是,這是一項付費服務​​。

0

谷歌已經改變了它的URL來

https://www.google.com/finance/converter?a 

嘗試這樣

function currency($from_Currency,$to_Currency,$amount) { 
      $amount = urlencode($amount); 
      $from_Currency = urlencode($from_Currency); 
      $to_Currency = urlencode($to_Currency); 
      $get = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency"); 
      $get = explode("<span class=bld>",$get); 
      $get = explode("</span>",$get[1]); 
      $converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]); 
      return round($converted_amount,2); 

      } 

    echo currency("USD","INR",2);