<?php error_reporting(0);
$currency_code = $_GET['currency_code'];
$currency_opt = strtoupper($currency_code)."INR";
$jsn_response = file_get_contents('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22' .$currency_opt. '%22%29&format=json&env=store://datatables.org/alltableswithkeys&callback=');
$currencyrate_arr = json_decode($jsn_response, true);
$currency_rate = $currencyrate_arr['query']['results']['rate']['Rate'];
//var_dump($currency_rate);
if($currency_rate > 0){
echo $currency_text = $currency_rate;
}
else{
echo $currency_text = "SORRY! ERROR..";
}
?>
它工作正常,但现在我得到错误,而使用这段代码的货币转换
脚本为我工作,并产生正确的结果。
如果你收到一个错误,要么是因为服务器配置,要么是因为API限制。
您可以查看https://developer.yahoo.com/yql/faq/
YQL中的速率限制基于您的身份验证。如果您使用基于IP的身份验证,那么您对公共YQL Web服务URL (/v1/public/*)的访问次数限制为每小时2000次/IP
如果您需要超过每小时2000个电话的限制,请阅读上面的链接以获取更多信息。
PS:如果你想调试、设置:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Edit:由于allow_url_fopen
被禁用,您不能在外部url上使用file_get_contents
,但您仍然可以使用CURL。
将file_get_contents
替换为:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22' .$currency_opt. '%22%29&format=json&env=store://datatables.org/alltableswithkeys&callback=');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$jsn_response = curl_exec($ch);
curl_close($ch);