2017-01-31 19 views
0

以前はデータベースのローカルアドレスを使用して経度と緯度を抽出するためのGoogleジオコードスクリプトがありました。URLにジオコーディングリクエストの読み込みエラーがありません

私はホストを交換しましたが、明らかにGoogleは新しいフォワードジオコーダを実装しました。これで、xmlスクリプト呼び出しからurl not loadエラーが返されます。

私は自分のコードを動作させるためにすべてを試しました。他のWebサイトからのサンプルコーディングも私のサーバーでは動作しません。私は何が欠けていますか?これが正しく実行されないようにするサーバー側の設定がありますか?

試み#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; 

何が原因で接続障害が発生する可能性がありますか?

答えて

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; 
関連する問題