• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Another things about postcode API

xoggoth

More fingers than teeth
Joined
Jul 8, 2005
Messages
17,131
Likes received
4,950
Thanks received
1,184
Location
xoggoth towers
Following zazou's post about postcode lookup APIs , thought I'd look into it. Found quite good thing here but documentation is sparse, they seem to assume you know all about API which I don't.

https://getaddress.io/Documentation

I understand what json is and a reference https://api.getAddress.io/v2/uk/SE192BB/14/?api-key={my api-key} produces a file 14.json with address details which I could then parse easily enough using file_get_contents in PHP.

What I can't figure out is how to get the content into the page, it keeps treating it as a download and opens it in Visual Studio!

Any tips (without using jquery/jfiddle etc) ? Ta.
 
The basic idea would be:

  • Get the content returned from the API endpoint (https://api.getAddress.io/v2/uk/SE192BB/14/?api-key={my api-key} in this case) into a variable - you say you're using PHP's file_get_contents, so presumably this bit is working. (An alternative would be to use PHP's curl functions.)

  • You now have the response as a JSON string which can be parsed into an object, so go ahead and do that using json_decode.

  • Now the response is an object, you can get the actual data out of it; this depends on how you want to display the data, as with any other object.

My PHP is a little rusty, but something like
PHP:
<?php
$postcode_api_json_response = file_get_contents('https://api.getAddress.io/v2/uk/SE192BB/14/?api-key={my api-key}');

// Have to make sure it's UTF-8; it probably is, but the call to utf8_encode is a belt-&-braces thing
$postcode_api_data = json_decode(utf8_encode($postcode_api_json_response));

// Example: Turn the latitude and longitude into a string
$postcode_lat_lon = $postcode_api_data->Latitude . ',' . $postcode_api_data->Longitude;

// Example: Get the array of addresses into a separate variable
$addresses_for_postcode = $postcode_api_data->Addresses;
?>
To display the addresses in an HTML select drop-down list using the variable from the last line above, something like:
PHP:
<label for='postcode'>Postcode:</label>
<select name='postcode' id='postcode'>
<?php foreach $address in $addresses_for_postcode: ?>
  <option><?= htmlspecialchars($address) ?></option>
<?php endforeach; ?>
</select>
should do the trick (barring any obvious syntax errors - I haven't actually run any of the above).

Note the use of htmlspecialchars: there's always the chance of something unexpected in any data you get from a remote service, so making sure it doesn't break anything on output is important!
 
Last edited: 12:28
Brill! Why do I need to search for such things on Google when I've got NickFitz on CUK? Works great, thanks!

I'm a lazy programmer so PHP works fine for me, always find I bang it together any old how and it works but thanks RSoles.
 
Brill! Why do I need to search for such things on Google when I've got NickFitz on CUK? Works great, thanks!

I'm a lazy programmer so PHP works fine for me, always find I bang it together any old how and it works but thanks RSoles.

x8vdMEF.jpg
 

Advertisers

Back
Top