我正在使用谷歌的计算器 API。我必须将动态值传递给 google url,如下所示:
$url = 'http://www.google.com/ig/calculator?hl=en&q=' . urlEncode($amount . $currency . '=?' . $exchangeIn);
但我从谷歌得到以下异常。
Warning: file_get_contents(http://www.google.com/ig/calculator?hl=en&q=13,000,000pkr=?cad) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable in /home/..../public_html/config/config.php on line 48
我的功能是:
function exchangeRate($amount, $currency, $exchangeIn) {
$url = 'http://www.google.com/ig/calculator?hl=en&q=' . urlEncode($amount . $currency . '=?' . $exchangeIn);
$data = file_get_contents($url);
if(!$data) {
throw new Exception('Could not connect');
}
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$array = $json->decode($data);
if(!$array) {
throw new Exception('Could not parse the JSON');
}
if($array['error']) {
throw new Exception('Google reported an error: ' . $array['error']);
}
return number_format($array['rhs']);
}
echo exchangeRate('9,200,000', 'pkr', 'cad')
有人可以我的代码有什么问题或这个 google api 有什么问题吗?
谢谢