我正在尝试使用以下curl从谷歌地图使用php获取一些数据
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);
然而,这是从google maps api返回的: stdClass对象 ( [error_message] =>此API项目无权使用此API。请确保在API控制台中激活此API:了解详情:https://code.google.com/apis/console [results] =>排列 ( )
[status] => REQUEST_DENIED
)
当我尝试通过命令行curl使用相同的url时,它可以工作。
curl -v https://maps.googleapis.com/maps/api/geocode/jsonaddress=Salt+Lake+City%2CUT&key=AIasasasasaasasg
我在php标题中看到以下内容:
> 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
而curl标题看起来像
CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googleapis.com
* start date: 2014-11-20 09:42:23 GMT
* expire date: 2015-02-18 00:00:00 GMT
* subjectAltName: maps.googleapis.com matched
* issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
* SSL certificate verify ok.
> GET /maps/api/geocode/json?address=Salt+Lake+City%2CUT HTTP/1.1
> User-Agent: curl/7.35.0
基本上GET是不同的。 有什么我可以做的工作吗?我已经设置api被任何引用者接受。是
答案 0 :(得分:1)
对于许多(令人沮丧的)小时,我遇到了同样的问题。你必须更换&#34;&amp;&#34;将参数与&
分开。
为什么它实际上与CLI curl(和任何浏览器)一起工作的原因我不知道:没有角色转义,&#39;键&#39;参数似乎根本没有被发送,但它使googlemaps api返回所请求的数据,就像一切都好......
答案 1 :(得分:0)
bad: GET /maps/api/geocode/json?address=Salt+Lake+City%2CUT
good: GET /maps/api/geocode/json?address=Salt+Lake+City%2CUT&key=AIasasasasassahg
显然,您缺少网址中的关键参数。
curl_setopt($ch,CURLOPT_URL, trim($_url));
从这段代码中看起来好像$_url
一定是个问题。