2017-01-31 32 views
0

我以前有一个Google地理编码脚本,用于使用数据库中的本地地址提取经度和纬度。Url没有加载地理编码请求的错误

在过去的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; 

简单的返回地址不加载。我尝试过使用和不使用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; 

任何想法,这可能是导致连接失败?

回答

0

我再也没有能够再次使用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;