Get WOEID Of A City Name From IP Address With PHP
Posted by in PHP July 19, 2011 10 Comments

This tutorial will use Yahoo! GeoPlanet API (or Web Service) to look up the WEOID of City name which is resolved from the IP Address. The tutorial has 2 parts:

  • 1. Look up the City from the current IP Address
  • 2. Use a City name as an input parameter of the web service to find the WOEID

If you already have a particular City to look up, you can ignore this part.

1. Get City From IP Address

Like other posts in my blog, I’m using GeoLite City from Maxmind and PHP API to get City from the current IP Address with PHP code below:

<?php
	include("geoipcity.inc");
	include("geoipregionvars.php");
 
	$ip = $_SERVER['REMOTE_ADDR'];
	$ip = "119.225.26.234";
 
	$gi = geoip_open("GeoLiteCity.dat",GEOIP_STANDARD);
	$record = geoip_record_by_addr($gi,$ip);
	geoip_close($gi);
 
	$city = $record->city;
 
	echo "City Name: " . $city . "<br>";
?>

2. Get WOEID From A City Name

First, you is required to get a Yahoo Application ID to use their Web Service. After register your application on the Yahoo! Developer Network, use the code below:

<?php
	$city = "Sydney";
 
	$url_post = "http://where.yahooapis.com/v1/places.q('".urlencode($city)."')?appid=foOF4CzV34EFIIW4gz1lx0Ze1em._w1An3QyivRalpXCK9sIXT5de810JWold3ApkdMdCrc-";
	$weather_feed = file_get_contents($url_post);
	$objDOM = new DOMDocument();
	$objDOM->loadXML($weather_feed);
	$woeid = $objDOM->getElementsByTagName("place")->item(0)->getElementsByTagName("woeid")->item(0)->nodeValue;
 
	echo "City Name: " . $city . "<br>";
	echo "WOEID: " . $woeid . "<br>";
?>

foOF4CzV34EFIIW4gz1lx0Ze1em._w1An3QyivRalpXCK9sIXT5de810JWold3ApkdMdCrc-” is my Yahoo Application ID, so please ensure you replace the app id with yours.

The response is XML format with sample is shown below:

<?xml version="1.0" encoding="UTF-8"?>
<places xmlns="http://where.yahooapis.com/v1/schema.rng" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:start="0" yahoo:count="1" yahoo:total="5">
	<place yahoo:uri="http://where.yahooapis.com/v1/place/1105779" xml:lang="en-us">
		<woeid>1105779</woeid>
		<placeTypeName code="7">Town</placeTypeName>
		<name>Sydney</name>
		<country type="Country" code="AU">Australia</country>
		<admin1 type="State" code="AU-NSW">New South Wales</admin1>
		<admin2/><admin3/>
		<locality1 type="Town">Sydney</locality1>
		<locality2/>
		<postal/>
		<centroid>
			<latitude>-33.869629</latitude>
			<longitude>151.206955</longitude>
		</centroid>
		<boundingBox>
			<southWest>
				<latitude>-34.189610</latitude>
				<longitude>150.517166</longitude>
			</southWest>
			<northEast>
				<latitude>-33.578140</latitude>
				<longitude>151.342575</longitude>
			</northEast>
		</boundingBox>
		<areaRank>6</areaRank>
		<popRank>13</popRank>
	</place>
</places>

Combine together with Full source code

<?php
	$start_process = (float) array_sum(explode(' ',microtime())); 
 
	include("geoipcity.inc");
	include("geoipregionvars.php");
 
	$ip = $_SERVER['REMOTE_ADDR'];
 
	$weather_feed = "";
 
	$gi = geoip_open("GeoLiteCity.dat",GEOIP_STANDARD);
	$record = geoip_record_by_addr($gi,$ip);
	geoip_close($gi);
 
	$city = $record->city;
 
	if ($city == "")
		$city = "Sydney";
 
	$url_post = "http://where.yahooapis.com/v1/places.q('".urlencode($city)."')?appid=foOF4CzV34EFIIW4gz1lx0Ze1em._w1An3QyivRalpXCK9sIXT5de810JWold3ApkdMdCrc-";
	$weather_feed = file_get_contents($url_post);
	$objDOM = new DOMDocument();
	$objDOM->loadXML($weather_feed);
	$woeid = $objDOM->getElementsByTagName("place")->item(0)->getElementsByTagName("woeid")->item(0)->nodeValue;
 
	echo "Current IP: " . $ip . "<br>";
	echo "City Name: " . $city . "<br>";
	echo "WOEID: " . $woeid . "<br>";
 
	$end_process = (float) array_sum(explode(' ',microtime())); 
    echo "<!--Execution time: ". sprintf("%.4f", ($end_process-$start_process))." seconds" . "-->";
?>

The output is similar with below:

Current IP: 114.141.196.80
City Name: Hunters Hill
WOEID: 26198454

Note:

  • 1. On line 18, you may assign a default City name to make sure our code works properly in case the GeoLiteCity.dat is out of date or City name is empty. Please download the latest GeoLiteCity.dat file from Maxmind or get a Pro version for more accuracy
  • 2. Again, please replace my Yahoo Application ID with yours, it’s for testing purpose only.

Download all source code above includes include files & current GeoLiteCity.dat file.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • Pingback: WOEID Of Eight Capital Cities In Australia | 4 Rapid Development

  • xxx

    thanks for the information, it helps me a lot

  • http://www.ajkergaan.com fahim

    Thanks… excellent job :)

  • http://www.flippingdude.com Ben

    I don’t know why its always showing the same city for me , i even tried to use some proxies , its always showing this: City Name: Brisbane !!!!!

  • http://www.hypnosis.com.au/tag/fear-of-poverty hypnotherapy mp3

    Remarkable issues here. I’m very satisfied to see your post. Thanks so much and I am having a look ahead to touch you. Will you please drop me a mail?

    • hoanhuynh

      Thanks for loving this post. If you need any further information, please send me a message via the contact form: http://4rapiddev.com/contact/

      Cheers.

  • http://thesharecashdownloader.tk Download Sharecash

    Magnificent points altogether, you just received a logo new reader. What would you recommend about your publish that you just made some days in the past? Any sure?

  • http://www.in.uk.com submit url

    Generally I do not learn article on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing taste has been surprised me. Thanks, quite great article.

  • http://tararadam.com/ tararadam

     Thank you for the nice tutorial!

  • Maria Niaz

    It seems great but I want to do this in android. Is there any way of doing so?