【问题标题】:Url not loading error on geocoding requests地理编码请求中的 URL 未加载错误
【发布时间】:2017-01-31 19:53:00
【问题描述】:

我之前有一个 Google 地理编码脚本,用于使用数据库中的本地地址提取经度和纬度。

在过去 6 个月中,我更换了主机,显然 Google 已经实施了新的正向地理编码器。现在它只是从 xml 脚本调用返回 url not loading 错误。

我已尽一切努力让我的代码正常工作。即使来自其他网站的示例编码也无法在我的服务器上运行。我错过了什么?是否有可能是服务器端设置阻止它正常执行?

尝试 #1:

$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?new_forward_geocoder=true&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";
echo $request_url;
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
return $status;

仅返回未加载的 url。我尝试过使用和不使用 new_forwad_geocoder。我也尝试过使用和不使用 https。

如果您只是将 $request_url 字符串复制并粘贴到浏览器,它会返回正确的结果。

也试过这个只是为了看看我是否可以得到一个文件来返回。尝试 2:

$request_url = "http://maps.googleapis.com/maps/api/geocode/json?new_forward_geocoder=true&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";//&sensor=true
echo $request_url."<br>";
$tmp = file_get_contents($request_url);
echo $tmp;

知道什么可能导致连接失败吗?

【问题讨论】:

    标签: php xml geocode google-geocoding-api


    【解决方案1】:

    我再也无法使用 XML 进行此操作,而 file_get_contents 调用是我几乎肯定的罪魁祸首。

    我已经发布了我使用 JSON/Curl(如下)所做的工作,以防有人遇到类似问题。

    最终,我认为我遇到的问题与升级到服务器上的 Apache 版本有关;并且一些与 file_get_contents 和 fopen 相关的默认设置更具限制性。不过我还没有确认。

    此代码确实有效:

    class geocoder{
        static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=";
    
        static public function getLocation($address){
            $url = self::$url.$address;
    
            $resp_json = self::curl_file_get_contents($url);
            $resp = json_decode($resp_json, true);
            //var_dump($resp);
            if($resp['status']='OK'){
              //var_dump($resp['results'][0]['geometry']['location']);
                //echo "<br>";
                //var_dump($resp['results'][0]['geometry']['location_type']);
                //echo "<br>";
                //var_dump($resp['results'][0]['place_id']);
    
                return array ($resp['results'][0]['geometry']['location'], $resp['results'][0]['geometry']['location_type'], $resp['results'][0]['place_id']);
            }else{
                return false;
            }
        }
    
        static private function curl_file_get_contents($URL){
            $c = curl_init();
            curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($c, CURLOPT_URL, $URL);
            $contents = curl_exec($c);
            curl_close($c);
    
            if ($contents) return $contents;
                else return FALSE;
        }
    }
    
    $Address = "1600 Amphitheatre Parkway, Mountain View, CA";
    $Address = urlencode(trim($Address));
    
    list ($loc, $type, $place_id) = geocoder::getLocation($Address);
    //var_dump($loc);
    $lat = $loc["lat"];
    $lng = $loc["lng"];
    echo "<br><br> Address: ".$Address;
    echo "<br>Lat: ".$lat;
    echo "<br>Lon: ".$lng;
    echo "<br>Location: ".$type;
    echo "<br>Place ID: ".$place_id;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 2011-07-20
      • 2020-06-07
      • 2016-02-23
      • 2011-03-18
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      相关资源
      最近更新 更多