Skip to content

Instantly share code, notes, and snippets.

@nickopris
Created March 6, 2021 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickopris/9ba351379bce008e2892948fc34f6953 to your computer and use it in GitHub Desktop.
Save nickopris/9ba351379bce008e2892948fc34f6953 to your computer and use it in GitHub Desktop.
Postcodes.io geolocation search by postcode
<?php
/**
* @param $postcode
*
* @return array
*/
function getGeoLocationFromPostcode($postcode) {
$geo = [];
$url = "https://api.postcodes.io/postcodes/" . $postcode;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
$json = json_decode($result);
$geo = [
'lat' => $json->result->latitude,
'lon' => $json->result->longitude,
];
}
return $geo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment