• 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!

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "Another things about postcode API"

Collapse

  • original PM
    replied
    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.

    Leave a comment:


  • xoggoth
    replied
    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.

    Leave a comment:


  • RSoles
    replied
    Other languages are available:-
    https://docs.python.org/2/library/json.html

    Leave a comment:


  • NickFitz
    replied
    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.

    Leave a comment:


  • xoggoth
    started a topic Another things about postcode API

    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.
Working...
X