duanli9569 2014-06-24 11:00 采纳率: 100%
浏览 52

实施雅虎货币转换器时出错

<?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..";     
    }
?>

It was working fine but now I am getting error while using this piece of code for currency conversion.

  • 写回答

1条回答 默认 最新

  • dongmi1995 2014-06-24 11:15
    关注

    The script is working for me, and produces correct results.

    If you are receiving an error, it's either because of a server configuration, or an API limit.

    You can check https://developer.yahoo.com/yql/faq/

    Rate limits in YQL are based on your authentication. If you use IP-based authentication, then you are limited to 2,000 calls/hour/IP to the public YQL Web service URL (/v1/public/*)

    If you need to exceed the 2000 calls per hour limit, read the above link for more information.

    PS: If you want to debug, set:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    Edit: Since allow_url_fopen is disabled, you can't use file_get_contents on outside URLs, but you can still use CURL.

    So just replace the file_get_contents with:

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

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错