Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将curl与Google calculator一起使用总是返回0_Php_Curl_Google Api - Fatal编程技术网

Php 将curl与Google calculator一起使用总是返回0

Php 将curl与Google calculator一起使用总是返回0,php,curl,google-api,Php,Curl,Google Api,我正在尝试使用curl和google calculator从以下url获取货币转换:。但以下代码始终返回0: <?php function currency($from_Currency, $to_Currency, $amount) { $amount = urlencode($amount); $from_Currency = urlencode($from_Currency); $to_Currency = urlencode($to_Currency);

我正在尝试使用curl和google calculator从以下url获取货币转换:。但以下代码始终返回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);
?>

有人能告诉我这里出了什么问题吗

谢谢。

。改为使用双引号,或将字符串合并为多个片段

$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;