【问题标题】:HTTP Request Failed - Google MapsHTTP 请求失败 - 谷歌地图
【发布时间】:2015-03-23 18:05:28
【问题描述】:

我对我的 Google Maps API 有疑问。我正在使用一个简单的地理编码脚本从我的服务器中提取一个 txt/xml 文件并对地址进行地理编码。我遇到了地址作为空值返回并破坏脚本和我的数据库的问题,因为 coord_lat 和 coord_long 是浮点数并且不能接受空值。

我收到的错误是:Warning: file_get_contents(http://maps.google.com/maps/geo?key=AIzaSyBmT9F_ixjLlW8oDiYVqgHqt14a008kXwc&output=xml&q=131+rochestor+ave+039482) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /includes/sfupdate/func.php on line 294 {"status":"Failed","data":"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'coord_lat' cannot be null"} ErrorSyntaxError: Unexpected token

这是我与 Google Maps API 连接的问题吗?我看到 HTTP 请求失败,这让我相信我被拒绝了,并且地址因此返回空值。建议将不胜感激,谢谢。

【问题讨论】:

    标签: php mysql http request geocode


    【解决方案1】:

    很抱歉发布新答案,但这是我当前的地理编码脚本。

    function get_lat_long($address, $zipcode) {
        if(strlen($zipcode) == 4){
            $zipcode = '0' . $zipcode;
            //echo $zipcode . "\n";
        }   
    
    
    
        $apikey = "IzaSyBmT9F_ixjLlW8oDiYVqgHqt14a008kXwc";
        $geourl = "http://maps.google.com/maps/geo?key=" . $apikey . "&output=xml&q=" . urlencode($address) . "+" . urlencode($zipcode);
        //echo $geourl;
        //echo "<br>";
    
        // Grab XML content using $geourl
        $context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
    
        $xml = file_get_contents($geourl, false, $context);
        $xml = simplexml_load_string($xml);
    
    
        //Parse the lat and long from the XML
        $Lat = $xml->results->result->locations->location->displayLatLng->latLng->lat;
        //echo $Lat;
        $Lng = $xml->results->result->locations->location->displayLatLng->latLng->lng;
        //echo $Lng;
    
    
        //Return
        if($Lng && $Lat) {
            return array('lat'=>$Lat,
                         'lng'=>$Lng
                         );          
        } else {
            return false;
        }
    
    
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      相关资源
      最近更新 更多