如何在 Php 中使用 google 货币 API 转换货币?
How to Convert Currency using google currency API in Php?
我在 php 中使用 google 货币转换 API 通过使用 file_get_content 但由于出现错误而无法获得输出,因此如何通过以下方式转换任何货币在 Php.
中使用以下 API
<?php
function convertCurrency($amount, $from, $to)
{
$url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to";
$data = file_get_contents($url);
preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
return $converted[1];
}
echo convertCurrency(1500, 'USD', 'INR');
?>
出现这样的错误
Message: file_get_contents(http://www.google.com/finance/converter?a=1500&from=USD&to=INR): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
几天前我已经回答了一个(代码和你的差不多)。
我鼓励你阅读我的 :
You're not calling an actual API, you're scraping a web page, which means that:
- you're most likely violating Google's TOS
- you're more likely to get rate-limited (or be detected as abuse and be blacklisted) at some point if you're fetching this page too often
这可能就是您在这里遇到的情况。您很可能已被列入黑名单。
解决方法:使用合适的 API 例如 OpenExchangeRates.
有点晚了,但它可能会对某些人有所帮助,
正如 Benjamin 所说
You're not calling an actual API, you're scraping a web page, which means that:
- you're most likely violating Google's TOS
- you're more likely to get rate-limited (or be detected as abuse and be blacklisted) at some point if you're fetching this page too often
代码片段
$url = "https://www.google.com/search?q=INR+to+USD";//Change Accordingly
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$data = explode("1 Indian Rupee = ",$result);//Change Accordingly
$one_inr_rate_to_usd = (float) substr($data[1],0,7);
function thmx_currency_convert($amount){
$url = 'https://api.exchangerate-api.com/v4/latest/USD';
$json = file_get_contents($url);
$exp = json_decode($json);
$convert = $exp->rates->USD;
return $convert * $amount;
}
echo thmx_currency_convert(9);
我在 php 中使用 google 货币转换 API 通过使用 file_get_content 但由于出现错误而无法获得输出,因此如何通过以下方式转换任何货币在 Php.
中使用以下 API<?php
function convertCurrency($amount, $from, $to)
{
$url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to";
$data = file_get_contents($url);
preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
return $converted[1];
}
echo convertCurrency(1500, 'USD', 'INR');
?>
出现这样的错误
Message: file_get_contents(http://www.google.com/finance/converter?a=1500&from=USD&to=INR): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
几天前我已经回答了一个
我鼓励你阅读我的
You're not calling an actual API, you're scraping a web page, which means that:
- you're most likely violating Google's TOS
- you're more likely to get rate-limited (or be detected as abuse and be blacklisted) at some point if you're fetching this page too often
这可能就是您在这里遇到的情况。您很可能已被列入黑名单。
解决方法:使用合适的 API 例如 OpenExchangeRates.
有点晚了,但它可能会对某些人有所帮助, 正如 Benjamin 所说
You're not calling an actual API, you're scraping a web page, which means that:
- you're most likely violating Google's TOS
- you're more likely to get rate-limited (or be detected as abuse and be blacklisted) at some point if you're fetching this page too often
代码片段
$url = "https://www.google.com/search?q=INR+to+USD";//Change Accordingly
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$data = explode("1 Indian Rupee = ",$result);//Change Accordingly
$one_inr_rate_to_usd = (float) substr($data[1],0,7);
function thmx_currency_convert($amount){
$url = 'https://api.exchangerate-api.com/v4/latest/USD';
$json = file_get_contents($url);
$exp = json_decode($json);
$convert = $exp->rates->USD;
return $convert * $amount;
}
echo thmx_currency_convert(9);