如何在 Php 中使用谷歌货币 API 转换货币?

Ano*_*ous 1 php api currency

我通过使用 file_get_content 在 php 中使用谷歌货币转换 API,但由于出现错误而无法获取输出,那么如何通过在 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');  
 ?> 
Run Code Online (Sandbox Code Playgroud)

出现这样的错误

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
Run Code Online (Sandbox Code Playgroud)

小智 8

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);
Run Code Online (Sandbox Code Playgroud)