谷歌地理编码API没有结果与php-CUrl


Google Geocoding API no results with php-CUrl

我有一个问题与谷歌地理编码API。当我去到url对应于我想要的api调用https://maps.googleapis.com/maps/api/geocode/json?address=6+QUAI+DE+LORIENT+94569+RUNGIS+CEDEX&key=[API_KEY]我有两个结果。当我通过php-CUrl询问它时,我得到了一个"No Result"的响应。在300个不同的地址上,我有几个这样的地址(最多15个)。我不知道为什么。

我尝试了一些事情:经典的旋度

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

Curl类(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);

使用和不使用user-agent/headers。谢谢!

独立于CURL或任何正在使用的请求方法,我建议总是在查询字符串的末尾添加国家代码,这样谷歌就知道在哪里寻找,而不是基于参数/requestheheaders猜测。

注意以下的区别:

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

第一个没有结果,第二个有。

给出的cURL代码的问题是您没有为请求设置足够的选项。你正在尝试从一个https地址检索,但是你的curl请求不承认这个。

你需要有一个cacert的副本。在系统的某个地方,使用curl中的cainfo选项来引用它。你可以下载cacert。pem这里

那么你需要在你的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 );