首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有php-CUrl的结果

没有php-CUrl的结果
EN

Stack Overflow用户
提问于 2015-07-29 07:53:31
回答 2查看 2.9K关注 0票数 2

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

我试了几样东西:经典卷曲

代码语言:javascript
复制
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);

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

代码语言:javascript
复制
$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);

有和没有用户代理/头。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-29 08:09:16

我建议总是在查询字符串的末尾添加一个国家代码,这样google就知道应该在哪里查找,而不是根据参数/请求头来猜测。

注意以下之间的区别:

代码语言:javascript
复制
$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));

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

票数 1
EN

Stack Overflow用户

发布于 2015-07-29 08:05:50

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

您需要在系统的某个地方拥有一个cacert.pem的副本,并使用cainfo选项(Cainfo)在curl中引用该副本。你可以在这里下载cacert.pem

然后,您需要在您的卷发配置中引用这一点。

代码语言:javascript
复制
    /* 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 );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31694445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档