Created
April 2, 2023 23:34
-
-
Save sumonst21/991fb54336bc8a8c01ac442a4d18ce74 to your computer and use it in GitHub Desktop.
Validate address using OpenStreetMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Validate address using OpenStreetMap | |
* | |
* @param string $street | |
* @param string $house_no | |
* @param string $city | |
* @param string $zip | |
* @param string $state | |
* @param string $country | |
* @return array | |
* @throws Exception | |
* | |
* @see https://nominatim.org/release-docs/develop/api/Search/ | |
* @author sumonst21 | |
*/ | |
function validateAddress($street='', $house_no = '', $city='', $zip='', $state='', $country='de') | |
{ | |
if (empty($street) || empty($house_no) || empty($city) || empty($zip) || empty($country)) { | |
throw new Exception('Incomplete address' . $street . ' ' . $house_no . ' ' . $city . ' ' . $zip . ' ' . $country); | |
} | |
$street = trim($street); | |
$house_no = trim($house_no); | |
$city = trim($city); | |
$zip = trim($zip); | |
$state = trim($state); | |
$country = trim($country); | |
$email = $email = Yii::app()->functions->getOptionAdmin('osm_email'); | |
$str = $street . '+' . $house_no; | |
//$str = "House No. $house_no, $street"; | |
$params = array( | |
'format' => 'jsonv2', | |
'addressdetails' => 1, | |
'email' => $email, | |
//'q' => $street . ' ' . $house_no . $city . $zip . $state . $country, | |
'street' => $str, | |
'postalcode' => $zip, | |
'country' => $country, | |
'city' => $city, | |
'state' => $state, | |
'limit' => 1, | |
'accept-language' => 'de,en', | |
'dedupe' => 1, | |
'countrycodes' => $country | |
); | |
$url = "https://nominatim.openstreetmap.org/search?" . http_build_query($params); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// use curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); if you get error: SSL certificate problem: unable to get local issuer certificate | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
$err = curl_error($ch); | |
curl_close($ch); | |
if ($err) { | |
throw new Exception($err . $url); | |
} | |
$data = json_decode($result, true); | |
if (empty($data) || !is_array($data) || count($data) == 0) { | |
//throw new Exception(Yii::t("default", "Please enter a valid street address" . $url)); | |
throw new Exception(Yii::t("default", "Please enter a valid street address")); | |
} | |
$valid_categories = array( | |
["apartments"],["barracks"],["bungalow"],["cabin"],["detached"],["dormitory"],["farm"],["ger"],["hotel"],["house"],["houseboat"],["residential"],["semidetached_house"],["static_caravan"],["stilt_house"],["terrace"],["tree_house"],["commercial"],["industrial"],["kiosk"],["office"],["retail"],["supermarket"],["warehouse"],["cathedral"],["chapel"],["church"],["kingdom_hall"],["monastery"],["mosque"],["presbytery"],["religious"],["shrine"],["synagogue"],["temple"],["bakehouse"],["bridge"],["civic"],["college"],["fire_station"],["government"],["gatehouse"],["hospital"],["kindergarten"],["public"],["school"],["toilets"],["train_station"],["transportation"],["university"],["barn"],["conservatory"],["cowshed"],["farm_auxiliary"],["greenhouse"],["slurry_tank"],["stable"],["sty"],["grandstand"],["pavilion"],["riding_hall"],["sports_hall"],["stadium"],["hangar"],["hut"],["shed"],["carport"],["garage"],["garages"],["parking"],["digester"],["service"],["transformer_tower"],["water_tower"],["storage_tank"],["silo"],["beach_hut"],["bunker"],["castle"],["construction"],["container"],["dog_house"],["military"],["roof"],["ruins"],["tent"],["yes"] | |
); | |
$valid_categories[] = 'leisure'; | |
$valid_categories[] = 'amenity'; | |
$valid_categories[] = 'shop'; | |
$valid_categories[] = 'office'; | |
$valid_categories[] = 'entrance'; | |
$valid_categories[] = 'building'; | |
$valid_categories[] = 'historic'; | |
if (isset($data[0]['category']) && !in_array($data[0]['category'], $valid_categories)) { | |
//throw new Exception(Yii::t("default", "Please enter a valid house number" . ' ' . $data[0]['category'])); | |
throw new Exception(Yii::t("default", "Please enter a valid house number, if you believe this is a mistake please contact us"), 406); | |
} | |
$gmap_address_with = $data[0]['lat'] . ',' . $data[0]['lon']; | |
$gmap_url = 'https://www.google.com/maps/place/' . $gmap_address_with; | |
return array( | |
'lat' => $data[0]['lat'], | |
'long' => $data[0]['lon'], | |
'city' => $data[0]['address']['town'], | |
'zip' => $data[0]['address']['postcode'], | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment