Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example code for using Postcoder in PHP. For more information on Postcoder, visit https://postcoder.com
<?php
/*
Postcoder example
Allies Computing Ltd
Demonstrates UK Address lookup rest web service
Product URL: https://postcoder.com
Technical Specs: https://developers.alliescomputing.com/postcoder-web-api/address-lookup/premise
Example output: http://ws.postcoder.com/pcw/PCW45-12345-12345-1234X/address/UK/NR147PZ
This demo shows how to perform an address lookup and parse the results into php objects.
Out of the box it uses a restricted API Key that limits the search to the postcode NR14 7PZ
To experiment with different search terms, signup for a free evaluation API/Search key at:
https://postcoder.com/sign-up
Alternatively the code below can be pasted into an online interpreter such as
http://www.compileonline.com/execute_php_online.php
Note: This script has been developed for php version >= 5.2.0
cURL and json_decode/json_encode functions are used.
*/
// API/search key - MUST be supplied to unlock search,
// if this is blank a restricted key is used which limits search to NR147PZ
$searchKey = ''; // replace with your search key
$searchterm = 'NR147PZ'; // string to use for an address search
echo 'Postcoder PHP Client Snippet<br><br>';
if(empty($searchKey)) {
$searchKey = 'PCW45-12345-12345-1234X';
$searchterm = "NR14 7PZ";
echo 'No search-key: using restricted evaluation key - resets search term to NR14 7PZ<br>'
. 'To obtain a free evaluation key, signup via'
. 'https://postcoder.com/sign-up<br>'
. '<br>';
}
// build the URL, using the 'address' search method:
$URL = 'https://ws.postcoder.com/pcw/' . $searchKey .'/address/UK/' . rawurlencode($searchterm) ;
// for other uri options see https://developers.alliescomputing.com/postcoder-web-api/address-lookup/premise
// use cURL to send the request and get the response
$session = curl_init($URL);
// Tell cURL to return the request data
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// use application/json to specify json return values, the default is XML.
$headers = array('Content-Type: application/json');
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
// Execute cURL on the session handle
$response = curl_exec($session);
// Close the cURL session
curl_close($session);
// decode the response
$addresses = json_decode($response);
// trivial output
echo 'Results for: ' . $searchterm . '<br><br>';
if (count($addresses) > 0){
foreach ($addresses as $address){
echo $address->summaryline,"<br>";
}
}
else{
echo 'No results, please try again.';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.