简体   繁体   English

Google Geocoding API 使用 php-CUrl 没有结果

[英]Google Geocoding API no results with php-CUrl

I have a problem with the Google Geocoding API.我的 Google Geocoding API 有问题。 When I go to the url corresponding to the api call I want https://maps.googleapis.com/maps/api/geocode/json?address=6+QUAI+DE+LORIENT+94569+RUNGIS+CEDEX&key=[API_KEY] I have 2 results.当我转到与 api 调用对应的 url 时,我想要https://maps.googleapis.com/maps/api/geocode/json?address=6+QUAI+DE+LORIENT+94569+RUNGIS+CEDEX&key=[API_KEY]我有 2 个结果。 When I ask It throught php-CUrl, I have a "No Result" response.当我通过 php-CUrl 询问它时,我有一个“无结果”的回应。 On 300 differents addresses, I have a few like it (15 at most).在 300 个不同的地址上,我有几个喜欢它(最多 15 个)。 I dont know why.我不知道为什么。

I tried a few things : classic Curl我尝试了一些东西:经典卷曲

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);

Curl Class ( https://github.com/php-curl-class/php-curl-class ):卷曲类( https://github.com/php-curl-class/php-curl-class ):

$curl->setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36');
$curl->setHeader('pragma', 'no-cache');
$curl->setHeader('accept-encoding', 'gzip, deflate, sdch');
$curl->setHeader('accept-langage', 'fr-FR,fr;q=0.8,en;q=0.6,en-US;q=0.4');
$curl->setHeader('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
$curl->setHeader('cache-control', 'no-cache');
$curl->setHeader('authority', 'maps.googleapis.com');
$curl->get($url);

With and without user-agent/headers.有和没有用户代理/标头。 Thanks!谢谢!

Independed from CURL or whatever request method is being used, I would suggest always adding a countrycode at the end of the query string so google knows where to look instead of guessing based on parameters / requestheaders.独立于 CURL 或正在使用的任何请求方法,我建议始终在查询字符串的末尾添加一个国家/地区代码,以便谷歌知道在哪里查看而不是根据参数/请求头进行猜测。

Notice the difference between:注意以下区别:

$result = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=6+QUAI+DE+LORIENT+94569+RUNGIS+CEDEX');
var_dump(json_decode($result));

$result = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=6+QUAI+DE+LORIENT+94569+RUNGIS+CEDEX,FR');
var_dump(json_decode($result));

First one does not give results, the second one does.第一个没有给出结果,第二个给出。

The problem with the cURL code given is that you do not have sufficient options set for the request.给出的 cURL 代码的问题是您没有为请求设置足够的选项。 You are attempting to retrieve from an https address but your curl request does not acknowledge this.您正在尝试从 https 地址检索,但您的 curl 请求不承认这一点。

You need to have a copy of cacert.pem somewhere on your system and refer to that using the cainfo option in curl.您需要在系统的某处拥有一份 cacert.pem 的副本,并使用 curl 中的 cainfo 选项来引用该副本。 You can download cacert.pem here你可以在这里下载 cacert.pem

Then you need to refer to this in your curl configuration.然后你需要在你的 curl 配置中引用这个。

    /* I would NOT recommend storing cacert in your document root but this is just as an example. */

    $cacert=$_SERVER['DOCUMENT_ROOT'].'/cacert.pem';
    if( !file_exists( realpath( $cacert ) ) exit('cant find certificate bundle');


    $url='https://maps.googleapis.com/maps/api/geocode/json?address=dundee,scotland';

    $curl=curl_init();
    curl_setopt( $curl, CURLOPT_URL, $url );

    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, realpath( $cacert ) );
    }
    curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE );
    curl_setopt( $curl, CURLOPT_FRESH_CONNECT, TRUE );
    curl_setopt( $curl, CURLOPT_FORBID_REUSE, TRUE );
    curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
    curl_setopt( $curl, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_OLDEST );
    curl_setopt( $curl, CURLOPT_MAXCONNECTS, 1 );
    curl_setopt( $curl, CURLOPT_FAILONERROR, FALSE );
    curl_setopt( $curl, CURLOPT_HEADER, FALSE );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 15 );
    curl_setopt( $curl, CURLOPT_TIMEOUT, 90 );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36' );
    curl_setopt( $curl, CURLINFO_HEADER_OUT, FALSE );
    curl_setopt( $curl, CURLOPT_NOBODY, TRUE );

    $payload=array_filter( array(
            'response'  =>  curl_exec( $curl ),
            'info'          =>  curl_getinfo( $curl ),
            'errors'        =>  curl_error( $curl )
        ) 
    );
    curl_close( $curl );
    print_r( $payload );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM