-->

How can I look up City and State using Zip or Post

2019-06-03 17:29发布

问题:

How can I look up City and State From a Zip or Postal Code throughout the world using an API or web service with PHP? I have tried Google Maps Ge coder API, which works fine for US and Canada but not for UK and Mexico for all countries. Also I used Yahoo API for this, which worked better than Google but against some Zip Codes and Postal codes it returns results from Different countries.

Does anybody know which API or database I can use if I have to get City and State/Province against US, UK, Canada and Mexican zip/postal codes?

回答1:

Well, not exactly sure this is what your looking for but I am working on something similar. I have been using the following free web services. User enters a country and Postal Code to be used in the web service calls.

Documentation: www.geonames.org/export/web-services.html

User needs to enter a country and Postal code. To get the list of countries: http://api.geonames.org/countryInfo?username=demo ** Register and replace demo with your registration id.

Using the country code selected from above service and the postal code, call: http://api.geonames.org/postalCodeLookupJSON?postalcode=23454&country=US&username=demo

Hope this helps.



回答2:

  $doc = new DOMDocument();
  $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address='.$_GET['Zip'].'@&sensor=true';
  $doc->load($url);
  $result = $doc->getElementsByTagName( "address_component" );
  $i=0;
  foreach( $result as $address_component )
  {
      $short_names = $address_component->getElementsByTagName( "long_name" );
      $short_name = $short_names->item(0)->nodeValue;
      if($i==1)
      {
        echo  $city=$short_name;
         break;  
      }
      $i++;

  }


回答3:

Here is a webservice to do just that

https://www.mashape.com/vanitysoft/uk-boundaries-io

..simply it allows obtaining GeoJson for googleMaps..query by single or combining UK Postal Code(ex. ZE1 0AE) ,Sector, District, City, and Wards Boundaries

for example a UK District "Manchester" .../boundary/uk?district=Manchester will get you the below GeoJson(all sector boundaries in this district...

you can use this for US zipcodes: https://www.mashape.com/vanitysoft/boundaries-io