How to get all UK addresses by postal code

How to get all UK addresses by postal code

I will show you how you can get all addresses of UK by postal code. For this i will used an API https://api.getaddress.io. First I will create account on getaddress.io and after created an account generate an API KEY and use that key in your API.

I'm using the following code to get UK addresses by postal code.

<?php
if(isset($_POST['postsubmit'])){
    $postalcode = $_POST['postal'];
    $apiURL = "https://api.getaddress.io/find/".$postalcode."?api-key=API KEY HERE";
    $response = get_web_page($apiURL);
    $resArr = array();
    $resArr = json_decode($response);
    echo "<pre>"; print_r($resArr); echo "</pre>";

    function get_web_page($url) {
        $options = array(
            CURLOPT_RETURNTRANSFER => true,   // return web page
            CURLOPT_HEADER         => false,  // don't return headers
            CURLOPT_FOLLOWLOCATION => true,   // follow redirects
            CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
            CURLOPT_ENCODING       => "",     // handle compressed
            CURLOPT_USERAGENT      => "test", // name of client
            CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
            CURLOPT_TIMEOUT        => 120,    // time-out on response
        ); 

        $ch = curl_init($url);
        curl_setopt_array($ch, $options);
        $content  = curl_exec($ch);
        curl_close($ch);
        return $content;
    }

?>

 

HTML Form

<div class="container">
  <h2>Get Address by Postal Code</h2>
  <form action="" method="post">
    <div class="form-group">
      <label for="Postal Code">Postal Code:</label>
      <input type="text" class="form-control" id="email" placeholder="Postal Code" name="postal">
    </div>
    <button type="submit" name="postsubmit" class="btn btn-primary">Submit</button>
  </form>
</div>

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment