Google Finance Converter API没有响应



我一直在尝试使用php中的Google Finance Converter转换货币。

我使用了以下代码。

$amount = 100;
$from_Currency = "INR";
$to_Currency = "BTC";
 $amount = urlencode($amount);
  $from_Currency = urlencode($from_Currency);
  $to_Currency = urlencode($to_Currency);
$get = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency&meta=ei%3DZsa7WeGkE4_RuASY95SQAw");
  $get = explode("<span class=bld>",$get);

  $get = explode("</span>",$get[1]);  
  $converted_amount = preg_replace("/[^0-9.]/", null, $get[0]);
  echo ceil($converted_amount);
 ?>

但是我遇到以下错误

Warning: file_get_contents(https://finance.google.com/finance/converter?a=100&from=INR&to=BTC&meta=ei%3DZsa7WeGkE4_RuASY95SQAw): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in F:Xampphtdocstestindex.php on line 11
Notice: Undefined offset: 1 in F:Xampphtdocstestindex.php on line 16
0

如何解决此错误?

当我的脚本出错时,上面对我有用,但是由于某种原因,转换是错误的。看来我要做的就是更新脚本中的URL;我现在有以下内容(我一次只能转换一种货币,但是您应该能够弄清楚如何适应!):

function convertCurrency($to){
    $url = "http://finance.google.com/finance/converter?a=1&from=GBP&to=$to";
    // Previously: $url = "http://www.google.com/finance/converter?a=1&from=GBP&to=$to";
    $request = curl_init();
    $timeOut = 0;
    curl_setopt ($request, CURLOPT_URL, $url);
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
    $response = curl_exec($request);
    curl_close($request);
    $regularExpression = '#<span class=bld>(.+?)</span>#s';
    preg_match($regularExpression, $response, $finalData);
    $rate = $finalData[0];
    $rate = strip_tags($rate);
    $rate = substr($rate, 0, -4);
    return $rate;
}

我希望这会有所帮助。
g

我也有同样的问题。我认为Google已经改变了输出结果的方式。尝试此(对我有用,今天在Cest(UTC 2)下午12.14进行测试)

  function convertCurrency($amount, $from, $to) {
     $url = 'http://finance.google.com/finance/converter?a=' . $amount . '&from=' . $from . '&to=' . $to;
     $data = file_get_contents($url);
         preg_match_all("/<span class=bld>(.*)</span>/", $data, $converted);
         $final = preg_replace("/[^0-9.]/", "", $converted[1][0]);
        return round($final, 3);
   }
   echo convertCurrency(1, 'EUR', 'USD');  // output: 1.195 
/* I got errors until i've changed this line:
   $final = preg_replace("/[^0-9.]/", "", $converted[1]); to:
   $final = preg_replace("/[^0-9.]/", "", $converted[1][0]);
   .. maybe it works for your code too
*/

尝试此
为我工作。
将其更改为:

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

最新更新