Created
February 20, 2015 13:45
-
-
Save stephenkeable-allies/c491ed4e74ab9cb524b1 to your computer and use it in GitHub Desktop.
Basic PHP Geocoding Example using PostCoder Web. Find out more at - http://developers.alliescomputing.com/postcoder-web-api/geocoding/position
This file contains 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 | |
// Check if POST request has been made | |
if($_SERVER['REQUEST_METHOD'] === 'POST') { | |
// Basic validation on the postcode field | |
if(array_key_exists('postcode', $_POST) and !empty($_POST['postcode'])) { | |
// Insert your PostCoder Web API Key here | |
// Sign up for free at www.alliescomputing.com/postcoder/sign-up | |
$pcw_api_key = "YOUR-API-KEY-HERE"; | |
// URL we are calling with API key and url encoded postcode | |
$pcw_geocode_url = "http://ws.postcoder.com/pcw/". $pcw_api_key ."/position/uk/" . rawurlencode($_POST['postcode']); | |
// Get and JSON decode the response from the service | |
$response = json_decode(file_get_contents($pcw_geocode_url)); | |
// Make sure the repsonse isn't empty | |
if(!empty($response)) { | |
// Dislay co-ordinates | |
echo "<p>Co-ordinates for <strong>" . $_POST['postcode'] . "</strong></p>"; | |
echo "<p>Latitude: ".$response[0]->latitude."<br>Longitude: ".$response[0]->longitude."<br>Easting: ".$response[0]->grideasting."<br>Northing: ".$response[0]->gridnorthing."</p>"; | |
} else { | |
// Display error message | |
echo "<p><strong>" . $_POST['postcode'] . "</strong> - could not be found</p>"; | |
} | |
} | |
} | |
// Basic POST submit form HTML below | |
?> | |
<form action="geocoding.php" method="post"> | |
<fieldset> | |
<label for="postcode">Postcode:</label> | |
<input type="text" name="postcode" id="postcode"> | |
</fieldset> | |
<input type="submit" value="Geocode"> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment