【发布时间】:2018-02-24 15:38:35
【问题描述】:
我一直在尝试使用 PHP 中的谷歌金融转换器转换货币。
我使用了以下代码。
$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:\Xampp\htdocs\test\index.php on line 11
Notice: Undefined offset: 1 in F:\Xampp\htdocs\test\index.php on line 16
0
如何解决这个错误?
【问题讨论】:
-
您在定义
$get时缺少一个值。我已经用精炼的代码发布了我的答案。 -
Google 金融转换器 API 不适用于某些货币代码。参考这个:finance.google.com/finance/converter比特币在这里也不起作用。所以使用php函数来转换它会很有用。
-
是的@sucharithagidla,我们必须使用finance.google.com,我也在使用它。我的代码也适用于 USD 到 BTC 的转换,但是当我尝试将 INR 转换为 BTC 时会出现该错误
-
昨天我们遇到了错误。让我知道解决方案
标签: php converter google-finance google-finance-api