實時匯率轉換介面的程式碼分享
阿新 • • 發佈:2019-02-15
這兩天一直在弄世界買家網的工具專欄,今天寫了匯率換算,找了很多的介面,看了網上很多人的經驗,xe的容易被封,google的不是實時的,而且google在內地訪問不是很好,丟包率比較高。
最好看到人家用了yahoo的介面,於是借鑑過來,貼出程式碼來分享給大家。
程式碼如下:
function getExchangeRate($from_Currency,$to_Currency) { $amount = urlencode($amount); $from_Currency = urlencode($from_Currency); $to_Currency = urlencode($to_Currency); $url = "download.finance.yahoo.com/d/quotes.html?s=".$from_Currency.$to_Currency."=X&f=sl1d1t1ba&e=.html"; $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); return $data[1]; } //呼叫方法 echo getExchangeRate("CNY","USD");
看下世界買家網的匯率轉換工具的效果吧:網址:http://www.buyerinfo.biz/tools/exchange/ 歡迎大家提供參考意見 匯率轉換工具