2

私はこのURLから通貨換算を取得するためにグーグル計算機でcurlを使用しようとしています:http ://www.google.com/ig/calculator?hl = en&q = 100RUB =?EUR 。ただし、次のコードは常に0を返します。

<?php

function currency($from_Currency, $to_Currency, $amount) {
    $amount = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency = urlencode($to_Currency);
    //www.google.com/ig/calculator?hl=en&q=1USD=?EUR
    $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, 2);                  

}
echo currency("USD", "EUR", 1);
?>

誰かがここで何が悪いのかを提案できますか?

ありがとうございました。

4

1 に答える 1

2

一重引用符を使用すると、変数は展開されません。代わりに二重引用符を使用するか、文字列を複数の部分に連結してください。

$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
//or
$url = 'http://www.google.com/ig/calculator?hl=en&q=' . $amount . $from_Currency . '=?' . $to_Currency;
于 2012-08-07T11:01:46.033 に答える