• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • 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

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Another things about postcode API

    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.
    bloggoth

    If everything isn't black and white, I say, 'Why the hell not?'
    John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

    #2
    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 Code:
    <?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 Code:
    <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 by NickFitz; 21 December 2016, 15:14.

    Comment


      #3
      Other languages are available:-
      https://docs.python.org/2/library/json.html

      Comment


        #4
        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.
        bloggoth

        If everything isn't black and white, I say, 'Why the hell not?'
        John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

        Comment


          #5
          Originally posted by xoggoth View Post
          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.

          Comment

          Working...
          X