PHPでGoogle Maps Geocoding APIで、郵便番号から緯度経度を取得

PHPGoogle Maps Geocoding APIで、郵便番号から緯度経度を取得を試みた。

 

https://developers.google.com/maps/documentation/geocoding/usage-limits?hl=ja

 

によると、一日2500リクエストは無料で使えるらしいので、リクエストをカウントして、一日のリクエスト制限をかける処理を作成している。

 

PHPでコードを書いた。  

/**
* @param type $address 郵便番号または住所
*/
function getGeocorgind($address) {
    $geoResponse = null;

    if (!$this->isOverRequestCount()) {
        $response = $this->Get("http://maps.googleapis.com/maps/api/geocode/json?              address=" . urlencode($address) . "&language=ja&sensor=false"); 
        $geoResponse = json_decode($response);
        $this->countUpRequestCount();
    }

    return $geoResponse;
}

 

function Get($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($ch);
    if(!curl_errno($ch)) {
         $header = curl_getinfo($ch);
         if ((0 != $header['http_code']) && 
             (200 != $header['http_code']) &&
             (201 != $header['http_code'])
         ) {
              $this->log->error($header['http_code']);
              $this->log->error($response);
        }
    }

    curl_close( $ch );

    return $response; 
}