<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Illuminati Karate, Inc. &#187; JSON</title>
	<atom:link href="http://illuminatikarate.com/blog/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://illuminatikarate.com</link>
	<description>creative web design, development and marketing</description>
	<lastBuildDate>Fri, 03 Feb 2012 20:13:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Free Webservice to Geocode IP Addresses Saves The Day</title>
		<link>http://illuminatikarate.com/blog/free-webservice-to-geocode-ip-addresses-saves-the-day/</link>
		<comments>http://illuminatikarate.com/blog/free-webservice-to-geocode-ip-addresses-saves-the-day/#comments</comments>
		<pubDate>Sat, 23 May 2009 06:03:06 +0000</pubDate>
		<dc:creator>George Huger</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Webservices]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://illuminatikarate.com/blog/?p=265</guid>
		<description><![CDATA[At Illuminati Karate we love a challenge. So when our client called needing two lists of 1500 IP addresses geocoded as quickly as possible, we rose to the occasion. With the help of PHP and a freely available web service we were able to complete the task in just [...]]]></description>
			<content:encoded><![CDATA[<p>At Illuminati Karate we love a challenge. So when our client called needing two lists of 1500 IP addresses geocoded as quickly as possible, we rose to the occasion. With the help of PHP and a freely available web service we were able to complete the task in just under an hour. Here&#8217;s how we did it.</p>
<p><span id="more-265"></span><br />
<h4>The Challenge</h4>
<p>Given 2 text files, each containing approximately 1500 anonymous IP addresses, we were to determine how many resolved to each US state. Separate counts were to be maintained for each list. The data was needed for a time sensitive report, and the clock was ticking.</p>
<h4>The Tools</h4>
<p>There are several freely available SQL databases which correlate IP addresses to their geographic location, such as <a href="http://www.iplocationtools.com/sql_database.php">this one</a> maintained by iplocationtools.com. If we&#8217;d been building a web app setting up a local instance would have made sense, but at 4.1 million rows it was going to take too long to get a local instance running.</p>
<p>What we needed was a web service to do the lookups for us. In the past, we&#8217;d been unable to find a free web service to fulfill this role, as most will not provide data beyond a country level. But today we were lucky, and Google quickly turned up the perfect solution: a free REST web service running the very same database from <a href="http://www.iplocationtools.com">iplocationtools.com</a>. Its exactly what it says on the tin: <a href="http://freegeoip.appspot.com/">a free IP geolocation webservice</a>. It gave us even more accuracy than we needed (cities and zipcodes!), and even better it was running on <a href="http://www.appspot.com" rel="nofollow">Google&#8217;s App Engine</a>, so we knew it could handle a heavy load.</p>
<p>With the primary problem solved, the only thing needed was a simple PHP script to read in the input files, query the web service for each IP, parse the JSON results, and maintain a total for each state.</p>
<h4>The Solution</h4>
<p>Thanks to PHP5&#8242;s excellent JSON support, the script to interact with the web service was trivial. I won&#8217;t bore you with the file I/O and tallying, but here&#8217;s the function which converts the IP address to its state.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p265code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2652"><td class="code" id="p265code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">function</span> getStateFromIp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$resp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/json_decode"><span style="color: #990000;">json_decode</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://freegeoip.appspot.com/json/'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/urlencode"><span style="color: #990000;">urlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$resp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">status</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// lookup failed</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$resp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">regioncode</span><span style="color: #339933;">;</span>		
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>      
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Take note of the the <a href="http://php.net/json_decode">json_decode</a> function. If you&#8217;re not already familiar with it I&#8217;d highly recommend taking a few minutes to familiarize yourself with <a href="http://php.net/manual/en/ref.json.php">PHP5&#8242;s JSON functions</a>. JSON is becoming a de facto standard for web services, so being able to painlessly convert to and from JSON in PHP is critical.</p>
<p>Finally, we simply used <a href="http://php.net/manual/en/function.print-r.php">print_r</a> to dump the array containing the per state totals, and used <a href="http://www.datawatch.com/_products/monarch_pro.php">Monarch</a> to pull the data into an Excel file which we then delivered to the customer. (<em>Note: under normal circumstances we would have reached a more elegant solution, but given the time constraints brute force was the name of the game.</em>)</p>
<h4>Conclusion</h4>
<p><a href="http://illuminatikarate.com/web-services/">On-demand web services</a> are fundamentally changing the way we interact with data. Projects which were formerly large, slow, and expensive can now be completed in a matter of hours or days for a fraction of the cost. Perhaps most importantly, anyone can get involved.</p>
<p>Its an exciting time, and we&#8217;re happy to be a part of it all. Thanks for reading!</p>
<p class="note"><em>If your company needs help with a custom dataset <a href="http://illuminatikarate.com/contact-us/" rel="nofollow">contact us</a> today. Even if you&#8217;re not sure exactly what to ask for, we can help fill in the blanks.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://illuminatikarate.com/blog/free-webservice-to-geocode-ip-addresses-saves-the-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

