jcadima
3/23/2017 - 9:28 PM

Query Google maps by ZIP and get City and State

Query Google maps by ZIP and get City and State

https://gist.github.com/mbmccormick/628268



<?php
    $zipcode = $_GET[zipcode];
    $url = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" . $zipcode;
    $geocode = simplexml_load_file($url);
    $city = $geocode->result->address_component[1]->long_name;
    for ($i = 2; $i < 6; $i++)
    {
        if ($geocode->result->address_component[$i]->type == "administrative_area_level_1")
        {
            $state = $geocode->result->address_component[$i]->short_name;
            break;
        }
    }