Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP Get WOEID Of A City Name From IP Address With PHP

Get WOEID Of A City Name From IP Address With PHP

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>";
?>

<?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>";
?>

<?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>

<?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" . "-->";
?>

<?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

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.

Jul 19, 2011Hoan Huynh
String To Upper Case In PHP, JavaScript And .Net (CSharp)Get Or Find Facebook Fan Page Id Number
You Might Also Like:
  • WOEID Of Eight Capital Cities In Australia
  • PHP IP Address To Country City Region Latitude And Longitude
  • Free Online Tools Get IP Address Location, Organization, ISP, Hostname, Country
  • How To Track Website With Multiple Google Analytisc Accounts
  • Add More Extra Informations Or Fields To WordPress User Profile
  • PHP Parse Title Description Keywords From A Website
  • PHP measure/ calculate execution time of loading page
  • PHP Ip to Country
  • Validate Email Address Format Using PHP Regular Expression preg_match
  • Add Hyperlinks To Yahoo! Answers
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago PHPDOMDocument, file_get_contents, getElementsByTagName, ip address, loadXML, Maxmind, WOEID, Yahoo, Yahoo Weather API1,855
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
21,917 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
19,746 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,583 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,093 views
C# Read Json From URL And Parse/Deserialize Json
9,557 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Research Paper Writing Service
  • Annotated Bibliography Example – How it Can Help You
  • Essay Writing Online Tips – How to Write Essays Online With Essay Proof Editing
  • Get Research Paper Assistance From Professional Help
  • Customized Essay Writing Agency – Why You Want It
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (62)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development