首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从php获取错误调用geocode

从php获取错误调用geocode
EN

Stack Overflow用户
提问于 2018-05-23 22:57:46
回答 1查看 471关注 0票数 0

我在php中使用了这两种方法来调用geocode api,这里有两种方法,

我的解决方案1,

代码语言:javascript
复制
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&sensor=false&key=".$google_api_key;

$resp_json = file_get_contents($url);

现在的解决方案2,是使用curl,

代码语言:javascript
复制
// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&sensor=false&key=".$google_api_key;

$resp_json = get_web_page($url);

// method
function get_web_page( $url, $cookiesIn = '' ){
        $options = array(
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => true,     //return headers in addition to content
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
            CURLINFO_HEADER_OUT    => true,
            CURLOPT_SSL_VERIFYPEER => false,     // Validate SSL Cert
            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
            CURLOPT_COOKIE         => $cookiesIn
        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $rough_content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );

        $header_content = substr($rough_content, 0, $header['header_size']);
        $body_content = trim(str_replace($header_content, '', $rough_content));
        $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m"; 
        preg_match_all($pattern, $header_content, $matches); 
        $cookiesOut = implode("; ", $matches['cookie']);

        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['headers']  = $header_content;
        $header['content'] = $body_content;
        $header['cookies'] = $cookiesOut;

        if($errmsg != ''){
            echo json_encode(" ERROR ". $errmsg);
        }

    return $body_content;
}

现在我的问题是,这两种解决方案都是在本地系统和我的linux服务器上工作的。但是在另一个生产服务器上不工作的事情是一样的。

以下是我的两个解决方案上的错误,

解决方案1的错误,

file_get_contents():php_network_getaddresses: getaddrinfo失败:中不知道的名称或服务

和解决方案2的错误,

无法解析主机: maps.googleapis.com;名称或服务不知道

我的谷歌密钥很好,在我的末端工作。我不知道我和cpanel有什么关系。

EN

回答 1

Stack Overflow用户

发布于 2018-05-23 23:02:48

不要使用cURL方法,使用file_get_contents()方法。确保允许生产服务器也使用API密钥,因为问题似乎不是。除非您的密钥被专门设置为通配符,否则它不会适用于多台服务器。

http://console.developers.google.com > API & auth > APIs >在Google下按下更多> Geocoding >启用api并获取您的密钥,因此代码将类似于

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50498607

复制
相关文章

相似问题

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