Google地理编码API使用php CUrl没有结果

Google地理编码API使用php CUrl没有结果,php,api,curl,geocoding,Php,Api,Curl,Geocoding,我对谷歌地理编码API有一个问题。 当我转到与我想要的api调用对应的url时 我有两个结果。当我通过php CUrl询问它时,我有一个“无结果”的回答。 在300个不同的地址上,我有几个喜欢它(最多15个)。我不知道为什么 我尝试了几件事: 经典卷曲 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($ch);

我对谷歌地理编码API有一个问题。 当我转到与我想要的api调用对应的url时 我有两个结果。当我通过php CUrl询问它时,我有一个“无结果”的回答。 在300个不同的地址上,我有几个喜欢它(最多15个)。我不知道为什么

我尝试了几件事: 经典卷曲

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

有或没有用户代理/头。
谢谢

给定的cURL代码的问题是您没有为请求设置足够的选项。您正试图从https地址检索,但您的curl请求未确认这一点

您需要在系统中的某个位置拥有cacert.pem的副本,并使用curl中的cainfo选项引用该副本。你可以

然后,您需要在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 );

与CURL或正在使用的任何请求方法无关,我建议总是在查询字符串的末尾添加countrycode,这样google就知道应该在哪里查找,而不是根据参数/请求头进行猜测

请注意以下两者之间的区别:

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

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

我建议在查询中添加国家代码。由于谷歌不知道该看哪个国家,我看到了很多“不匹配”。所以:奇怪的是,当我用Google Chrome处理url时,我有两个结果。。。我将尝试使用国家代码当我在浏览器中输入准确的URL时,它也会给我0个结果。。。有了国家代码,我的效果很好。多谢各位!
$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));