使用php-CUrl

时间:2015-07-29 07:53:31

标签: php api curl geocoding

我的Google地理编码API存在问题。 当我转到对应于我想要的api电话的网址时 https://maps.googleapis.com/maps/api/geocode/json?address=6+QUAI+DE+LORIENT+94569+RUNGIS+CEDEX&key=[API_KEY]我有2个结果。当我通过php-CUrl询问它时,我有一个“没有结果”的回复。 在300个不同的地址上,我有一些喜欢它(最多15个)。我不知道为什么。

我尝试了一些事情: 经典卷曲

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

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

使用和不使用用户代理/标头。 谢谢!

2 个答案:

答案 0 :(得分:1)

独立于CURL或正在使用的任何请求方法,我建议始终在查询字符串的末尾添加国家/地区代码,以便google知道在哪里查看,而不是根据参数/请求标题进行猜测。

注意:

之间的区别
AnnotationRulerColumn$5 {}@140230138540928
  Style: NO_FOCUS | LEFT_TO_RIGHT | DOUBLE_BUFFERED 
  Layout Data: null
  Bounds: Rectangle {44, 0, 9, 206}

Children:

Peers:
  *AnnotationRulerColumn$5 {}@140230138540928 Layout Data: null Bounds: Rectangle {44, 0, 9, 206}
  AnnotationRulerColumn$5 {}@140230141853568 Layout Data: null Bounds: Rectangle {0, 0, 12, 206}
  LineNumberRulerColumn$4 {}@140230140489248 Layout Data: null Bounds: Rectangle {12, 0, 32, 206}

Parent Tree:
[CUT]

                                                       Canvas {}@139916372127008
                                                         Style: LEFT_TO_RIGHT | DOUBLE_BUFFERED 
                                                         Bounds: Rectangle {0, 0, 954, 276}
                                                         Layout: org.eclipse.jface.text.source.SourceViewer$RulerLayout@4a767290
                                                         LayoutData: null
                                                        CompositeRuler$CompositeRulerCanvas {}@139916355542160
                                                             Style: LEFT_TO_RIGHT | DOUBLE_BUFFERED 
                                                             Bounds: Rectangle {0, 0, 29, 258}
                                                             Layout: org.eclipse.jface.text.source.CompositeRuler$RulerLayout@76b6b86f
                                                             LayoutData: null

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

答案 1 :(得分:0)

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

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

然后你需要在你的卷曲配置中引用它。

    /* 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 );