Yahoo! Developer Network Blog
« Previous | Main | Next »
May 21, 2009
Geolocating web sites with Yahoo! Placemaker
Yahoo Placemaker is a new web service from the Yahoo Geo team. It is a location extractor service that returns all the known geographical locations from text, RSS feed, or an HTML document.
It is pretty easy to use the service and here's a quick step-by-step guide to extract the locations of a web site using PHP and cURL. You can see the demo and try it out for yourself.
In order to get the locations from a web URL you need to call the API endpoint http://wherein.yahooapis.com using POST and provide four parameters:
- the location of the web document as
documentURL, for examplehttp://uk.yahoo.com, - the
documentTypewhich could be text/plain, text/html, text/xml, text/rss, application/xml or application/xml+rss, - the format of the document you want back (XML or RSS) as
outputTypeand - your application ID.
In PHP using cURL it looks like this:
<?php
$key = 'PASTE YOUR API KEY HERE';
$apiendpoint = 'http://wherein.yahooapis.com/v1/document';
$url = 'http://uk.yahoo.com';
$inputType = 'text/html';
$outputType = 'xml';
$post = 'appid='.$key.'&documentURL='.$url.
'&documentType='.$inputType.'&outputType='.$outputType;
$ch = curl_init($apiendpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$results = curl_exec($ch);
header('content-type:text/xml');
echo $results;
?>
You can try it out for yourself and you'll see that the returned XML has all kinds of information explained in detail in the docs.
The interesting bits are really the places that the API found, and they are listed in the placeDetails element containing another place element:
Using simplexml you can easily turn these into PHP objects and loop over them. Be aware that the XML uses CDATA around the content, so simplexml_load_string needs to be told that.
<?php
$key = 'PASTE YOUR API KEY HERE';
$apiendpoint = 'http://wherein.yahooapis.com/v1/document';
$url = 'http://uk.yahoo.com';
$inputType = 'text/html';
$outputType = 'xml';
$post = 'appid='.$key.'&documentURL='.$url.
'&documentType='.$inputType.'&outputType='.$outputType;
$ch = curl_init($apiendpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$results = curl_exec($ch);
$places = simplexml_load_string($results, 'SimpleXMLElement',
LIBXML_NOCDATA);
echo '<h2>Results</h2>';
if($places->document->placeDetails){
echo '<table>';
echo '<caption>Locations for '.$url.'</caption>';
echo '<thead>';
echo '<th scope="row">Name</th>';
echo '<th scope="row">Type</th>';
echo '<th scope="row">woeid</th>';
echo '<th scope="row">Latitude</th>';
echo '<th scope="row">Longitude</th>';
echo '</thead>';
echo '<tbody>';
foreach($places->document->placeDetails as $p){
echo '<tr>';
echo '<td>'.$p->place->name.'</td>';
echo '<td>'.$p->place->type.'</td>';
echo '<td>'.$p->place->woeId.'</td>';
echo '<td>'.$p->place->centroid->latitude.'</td>';
echo '<td>'.$p->place->centroid->longitude.'</td>';
echo '</tr>';
}
echo '</tbody></table>';
} else {
echo '<h2>Couldn\'t find any locations for '.$url.'</h2>';
}
?>
You can try this out for yourself too, and adding a few more lines of code and a bit of styling makes it easy to use for any URL.
Another interesting part of the XML is the referenceList which gives you all the matches Placemaker found in the document and in the case of an HTML document, the XPATH pointing to them.
Happy Hacking!
Chris Heilmann
Yahoo Developer Network
Posted at May 21, 2009 7:08 AM | Permalink
Comments
Nice Post
Informative and Useful One
Thanks for great stuff
Posted by: php web development at July 31, 2009 2:16 AM
Post a comment
Comment Policy: We encourage comments and look forward to hearing from you. Please note that Yahoo! may, in our sole discretion, remove comments if they are off topic, inappropriate, or otherwise violate our Terms of Service. Fields marked with asterisk '*' are required.
Subscribe
Recent Blog Articles
view all
YQL Open Table for Google Buzz now live
Tue, 09 Feb 2010
INSERT INTO twitter.status ...
Mon, 08 Feb 2010
Announcing the Yahoo! Brasil Open Hack Day 2010, 20-21 March
Mon, 08 Feb 2010
Marketing hacks, linchpins, and tech women of valor
Sun, 07 Feb 2010
Yahoo! India invites you to join the first India Hadoop Summit
Thu, 04 Feb 2010
Recent Links
Appcelerator Titanium + Yahoo YQL on Vimeo
Mon, 08 Feb 2010
Tue, 02 Feb 2010
PhoneGap | Cross platform mobile framework
Sat, 30 Jan 2010
Web developers can rule the iPad - O'Reilly Radar
Sat, 30 Jan 2010
rc3.org - Is the iPad the harbinger of doom for personal computing?
Thu, 28 Jan 2010
Archives
2010
2009
2008
2007
2006
2005
Recent Readers


