2017-01-31 25 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;