php curl失败,而命令行curl工作

php curl失败,而命令行curl工作,php,google-maps,curl,Php,Google Maps,Curl,我正在尝试使用下面的curl来使用php从googlemap获取一些数据 curl_setopt($ch,CURLOPT_URL, trim($_url)); curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.35.0"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($

我正在尝试使用下面的curl来使用php从googlemap获取一些数据

    curl_setopt($ch,CURLOPT_URL, trim($_url));
    curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.35.0");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

    curl_setopt($ch,CURLOPT_POST, 0);
    $output=curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
[status] => REQUEST_DENIED
)
然而,google maps api返回了以下内容: stdClass对象 ( [错误消息]=>此API项目无权使用此API。请确保在API控制台中激活此API:了解详细信息: [结果]=>数组 ( )

[status] => REQUEST_DENIED
)
当我试图通过命令行curl使用相同的url时,它会起作用

[status] => REQUEST_DENIED
)
curl -v https://maps.googleapis.com/maps/api/geocode/jsonaddress=Salt+Lake+City%2CUT&key=AIasasasasaasasg
我在php标题中看到以下内容:

[status] => REQUEST_DENIED
)
> GET /maps/api/geocode/json?address=Salt+Lake+City%2CUT&key=AIasasasasassahg HTTP/1.1
 User-Agent: curl/7.35.0
 Host: maps.googleapis.com
Accept: */*


< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Date: Sat, 06 Dec 2014 14:14:13 GMT
< Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Cache-Control: no-cache, must-revalidate
< Vary: Accept-Language
< Access-Control-Allow-Origin: *
* Server mafe is not blacklisted
< Server: mafe
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Alternate-Protocol: 443:quic,p=0.02
< Transfer-Encoding: chunked
< 
* Connection #0 to host maps.googleapis.com left intact
基本上,GET是不同的。 我可以做些什么来让它工作吗?我已经将api设置为可以被任何推荐人接受。是吗

[status] => REQUEST_DENIED
)
bad:  GET /maps/api/geocode/json?address=Salt+Lake+City%2CUT  
good: GET /maps/api/geocode/json?address=Salt+Lake+City%2CUT&key=AIasasasasassahg
显然,您的URL中缺少关键参数

[status] => REQUEST_DENIED
)
curl_setopt($ch,CURLOPT_URL, trim($_url));

从这段代码来看,它看起来像一个坏的
$\u url
一定是问题所在。

我有很多(令人沮丧的)小时都遇到了同样的问题。你必须用
&;
替换分隔参数的“&”

[status] => REQUEST_DENIED
)
我不知道它为什么能与CLI curl(以及任何浏览器)一起工作:如果没有字符转义,“key”参数似乎根本不会被发送,但它会使googlemaps api返回请求的数据,就像一切正常一样

[status] => REQUEST_DENIED
)