dtv7174 2013-10-09 09:16
浏览 51

谷歌的计算器API不起作用?

I am using Google’s Calculator API. I have to pass dynamic values to the google url as below:

 $url = 'http://www.google.com/ig/calculator?hl=en&q=' . urlEncode($amount . $currency . '=?' . $exchangeIn);

But I am getting the following exception from the google.

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

My function for this is as:

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')

Can somebody what is wrong with my code or something wrong with this google api ?

Thanks

  • 写回答

1条回答 默认 最新

  • doujie7497 2013-10-09 09:38
    关注

    I also created currency calculator using Google’s Calculator API like that. Below is my code.

    Usage:

    simply use your own URL like: yourdomain.com/file.php?from=USD&to=EUR&q=10

    The PHP Script:

    <?php
    session_start();
    if(isSet($_GET['from']) && isSet($_GET['to']) && isSet($_GET['q'])) {
      $from = preg_replace('/[^A-Z]/','',$_GET['from']);
      $to = preg_replace('/[^A-Z]/','',$_GET['to']);
      $q = preg_replace('/[^0-9\.]/','',$_GET['q']);
    
      echo currency($from,$to,$q);
    }
    function currency($from_Currency,$to_Currency,$amount) {
      $cookieName = md5("$from_Currency|$to_Currency|$amount");
      if(isSet($_COOKIE["$cookieName"])) {
        echo $_COOKIE["$cookieName"];
      }
      else {
        $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 , $_SERVER['HTTP_USER_AGENT']);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $rawdata = curl_exec($ch);
        curl_close($ch);
    
        $data = html_entity_decode($rawdata);
        $data = str_replace(array('lhs','rhs','error','icc',': '),array('"lhs"','"rhs"','"error"','"icc"',':'),$data);
    
    
        $row = json_decode($data,true);
        if(preg_match('/million/',$row['rhs'])) {
          $x = 1000000;
        }
        else if(preg_match('/billion/',$row['rhs'])) {
          $x = 1000000000;
        }
        else {
          $x = 1;
        }
        $result = preg_replace('/[^0-9\.]/','',$row['rhs']);
        $kurs = $result * $x;
        if($row['icc'] == 'true') {
          setcookie("$cookieName", number_format($kurs,2), time()+3600);
          return number_format($kurs,2);
        }
        else {
          return "Not available";
        }
      }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本