1

我的 PHP 脚本有问题。如果我将少于 20 美元转换为 INR,它工作正常,但如果我使用超过 19(美元),那么它只显示结果输出的第 1 位。我想要完整的结果输出来使用这个脚本。如果有人帮助我解决这个问题,我感到很高兴。

这是代码:

function currency($from_Currency,$to_Currency,$amount) {
    $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 , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    $data = explode('"', $rawdata);
    $data = explode(' ', $data['3']);
    $var = $data['0'];
    return round($var,3);
}

$from_Currency ="USD";
$to_Currency ="INR";
$amount ="20";

echo currency($from_Currency,$to_Currency,$amount);
4

3 回答 3

2

试试这个

$data = explode('"', $rawdata);
preg_match_all("/([0-9]+(\.[0-9]+)?\s?[kBM]?)/", $data[3], $match1);
$var = implode(",", $match1[0]);
$var = str_replace(",", "", $var);
return round($var, 3);
于 2012-10-31T07:43:40.260 回答
0

我修改了你的代码。这工作如下所示的功能...

function currency($from_Currency,$to_Currency,$amount) {
    $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 , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    $data = preg_replace("/((\"?[^\"]+\"?)[ ]*:[ ]*([^,\"]+|\"[^\"]*\")(,?))/i", '"\\2": \\3\\4', str_replace(array('{', '}'), array('',''), $rawdata));
    return json_decode( '{' . $data . '}' );
}

$from_Currency ="USD";
$to_Currency ="INR";
$amount ="20";

$amt = currency($from_Currency,$to_Currency,$amount);
//print_r($amt);

// The actual converted amount can be obtained with
$converted_amount = $amt->rhs;
于 2012-10-31T07:30:42.957 回答
0

那是因为如果 rhs 值 > 1000,谷歌似乎会返回一个奇怪的数字。打印整个响应,您将看到。返回的值是 18 美元的 969 和 20 美元的 1á082.30965。我不认识那里的额外字符,也没有可用的文档。

使用解决方法 - 转换 1 个单位,然后乘以数量。

于 2012-10-31T07:34:06.523 回答