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

2019-06-03 17:10发布

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?

3条回答
贼婆χ
2楼-- · 2019-06-03 17:32

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.

查看更多
萌系小妹纸
3楼-- · 2019-06-03 17:40
  $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++;

  }
查看更多
Deceive 欺骗
4楼-- · 2019-06-03 17:49

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

enter image description here

查看更多
登录 后发表回答