通过GOOGLE(谷歌) API获取实时货币汇率(人民币和美金)的PHP代码

≡通过GOOGLE(谷歌) API获取实时货币汇率(人民币和美金)的PHP代码

调用方法:

echo $this->currency("USD", "CNY", 1);die;


当前(2016年6月11日汇率是6.56)

2016-06-11_210151.jpg

函数:

function currency($from_Currency, $to_Currency, $amount) {

    $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);


    $matches = array();

    preg_match_all("|<span class=bld>(.*)</span>|U", $rawdata, $matches);


    $result = explode(" ", $matches[1][0]);


    return round($result[0], 2);

}

2016-06-11_205917.jpg


≡在线申请ASI

参考价: $0

美国ASI协会方案
+¥5500
+¥4500
+¥40000
+¥900
+¥1200