dqyuipw44576 2014-11-23 07:17
浏览 85
已采纳

使用Google和CURL php进行货币转换

I am trying to convert currency data using Google API and PHP curl. unfortunately i am having the following problem and am not able to solve it still. can any one help me please. here is my PHP function.

function currency($from_Currency,$to_Currency,$amount) {
    $amount = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency = urlencode($to_Currency);
    $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
    $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);
    $data = explode(' ', $data['3']);
    $var = $data['0'];
    return round($var,2);
}

// here is my function call

$usd= currency("USD","ETB",1);
echo "270 USD= ".$usd."ETB";

but i am having the follwing error

Notice: Undefined offset: 3 ... on line .... (14 of this function)

  • 写回答

2条回答 默认 最新

  • doutidi5037 2014-11-23 07:58
    关注

    It seems your API URL is wrong because when $rawdata echo-ed, Google returning Error 404. For now, the available solution (if you're insist to use Google) is to convert currency using Google Finance. For example, using GET request, you can send a request with the following format:

    $url = "https://www.google.com/finance/converter?a=" . $amount . "&from=" . $from . "&to=" . $to;
    

    Unfortunately, Google return a full page HTML, so you need to parse the result manually. Just now I'm trying to access it using https://www.google.com/finance/converter?a=1&from=IDR&to=USD, and the conversion result is attached in a <div> like this:

    <div id=currency_converter_result>1 IDR = <span class=bld>0.0001 USD</span>
    

    So, if you save the result in a $rawdata variable, you can use regular expression in PHP to obtain the conversion result. Since it's not a formal API, you need to actively watching the page structure next time if the code doesn't work.

    This is your code, updated with a test:

    function convertCurrency($amount, $from_Currency, $to_Currency) {
        $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);
    
        preg_match("/<span class=bld>(.*)<\/span>/", $rawdata,  $converted);
        $converted = preg_replace("/[^0-9.]/", "", $converted);
    
        return round($converted[0], 3);
    }
    
    $usd = currency("USD", "ETB", 270);
    echo "270 USD = " . $usd . " ETB";
    

    My suggestion is: find another API for currency conversion. You can look for alternative like https://openexchangerates.org/. Unfortunately, it's a paid service.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题