<?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>code-diesel &#187; curl</title>
	<atom:link href="http://www.codediesel.com/tag/curl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Parallel cURL execution in PHP</title>
		<link>http://www.codediesel.com/php/parallel-curl-execution/</link>
		<comments>http://www.codediesel.com/php/parallel-curl-execution/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 07:15:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[parallel]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2678</guid>
		<description><![CDATA[executing curl requests in parallel in php]]></description>
			<content:encoded><![CDATA[<p>cURL is a Swiss army knife of web content processing. Programmers use it for a number of things  everyday. One interesting feature the cURL library offers that many programmers are unaware of is the parallel execution of requests.<br />
curl has two major modes of request execution: &#8216;easy&#8217; mode and the &#8216;multi&#8217; mode. Most people use the &#8216;easy&#8217; mode &#8211; in this mode when we issue multiple requests, the second request will not start until the first one is complete. This is known as synchronous execution, and this is the one we normally use. This means that if we have 100 requests to process, each will be processed in a linear manner, which could take a lot of time. This is where &#8216;multi&#8217; mode comes to the rescue. In this mode all requests can be handled in parallel or asynchronously. And it can be quite handy and time saving on many occasions. The &#8216;multi&#8217; or parallel mode is handled by the following curl functions:</p>

<div class="wp_codebox"><table><tr id="p26781"><td class="code" id="p2678code1"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">curl_multi_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_multi_add_handle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_multi_select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_multi_exec</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_multi_getcontent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_multi_info_read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_multi_remove_handle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span id="more-2678"></span></p>
<h4>Using parallel curl libraries</h4>
<p>But there is one caveat when using the multi mode; it can be a little confusing. And as always, working with asynchronous code is not always easy, so I prefer to use classes developed by other people. Between the various libraries I&#8217;ve found on the net, <a target="_blank" href="http://github.com/petewarden/ParallelCurl">pete wardens</a> is the one I like. Below is a rough outline of how it works. You first initialize the class and specify the number of requests it should handle in parallel (10 here) along with some curl options.</p>

<div class="wp_codebox"><table><tr id="p26782"><td class="code" id="p2678code2"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$parallel_curl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ParallelCurl<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$curl_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next we call the <em>startRequest</em>method, specifying the url to request and a callback function, which will be run when the request is processed. We can also pass a optional third parameter which will be handed over to the callback function. If there are any post fields, you can pass them as the fourth parameter in the <em>startRequest</em>method.</p>

<div class="wp_codebox"><table><tr id="p26783"><td class="code" id="p2678code3"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$parallel_curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">startRequest</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_url</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'on_request_done'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$search</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Thats it, this is all that is required to execute parallel curl requests. Below is a broad overview of how to use the class.</p>

<div class="wp_codebox"><table><tr id="p26784"><td class="code" id="p2678code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'parallelcurl.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.example.com/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.example.com/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url3</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.example.com/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// This function gets called back for each request that completes</span>
<span style="color: #000000; font-weight: bold;">function</span> on_request_done<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #000088;">$search</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$max_requests</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$curl_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    CURLOPT_SSL_VERIFYPEER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
    CURLOPT_SSL_VERIFYHOST <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$parallel_curl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ParallelCurl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$max_requests</span><span style="color: #339933;">,</span> <span style="color: #000088;">$curl_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Start 3 parallel requests. All three will be started simultaneously.</span>
<span style="color: #000088;">$parallel_curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">startRequest</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'on_request_done'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$parallel_curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">startRequest</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'on_request_done'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$parallel_curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">startRequest</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'on_request_done'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$parallel_curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">finishAllRequests</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The complete test script using the class to run Google search queries in parallel is shown below.</p>

<div class="wp_codebox"><table><tr id="p26785"><td class="code" id="p2678code5"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'parallelcurl.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SEARCH_URL_PREFIX'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://ajax.googleapis.com/ajax/services/\
                            search/web?v=1.0&amp;rsz=large&amp;filter=0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// This function gets called back for each request that completes</span>
<span style="color: #000000; font-weight: bold;">function</span> on_request_done<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #000088;">$search</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$httpcode</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_getinfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLINFO_HTTP_CODE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$httpcode</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">print</span> <span style="color: #0000ff;">&quot;Fetch error <span style="color: #006699; font-weight: bold;">$httpcode</span> for '<span style="color: #006699; font-weight: bold;">$url</span>'<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$responseobject</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$responseobject</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'responseData'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'results'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">print</span> <span style="color: #0000ff;">&quot;No results found for '<span style="color: #006699; font-weight: bold;">$search</span>'<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">print</span> <span style="color: #0000ff;">&quot;********<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$search</span>:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">print</span> <span style="color: #0000ff;">&quot;********<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$allresponseresults</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$responseobject</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'responseData'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'results'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$allresponseresults</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$responseresult</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$responseresult</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$title</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// The terms to search for on Google</span>
<span style="color: #000088;">$terms_list</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;Paul&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Lena&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;Lee&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Marie&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;Tom&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Ada&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;Herman&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Josephine&quot;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$max_requests</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$max_requests</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$curl_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    CURLOPT_SSL_VERIFYPEER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
    CURLOPT_SSL_VERIFYHOST <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
    CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #0000ff;">'Parallel Curl test script'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$parallel_curl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ParallelCurl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$max_requests</span><span style="color: #339933;">,</span> <span style="color: #000088;">$curl_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms_list</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$terms</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$search</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$terms</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' is a&quot;'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$search_url</span> <span style="color: #339933;">=</span> SEARCH_URL_PREFIX<span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;q='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$parallel_curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">startRequest</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_url</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'on_request_done'</span><span style="color: #339933;">,</span><span style="color: #000088;">$search</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$parallel_curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">finishAllRequests</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<blockquote><p>
Note: I have found the php curl multi mode execution to be inconsistent on Windows. But it works great on Linux (I&#8217;m using Ubuntu).
</p></blockquote>
<p>You can download the parallelCurl class from <a target="_blank" href="http://github.com/petewarden/ParallelCurl">gitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/parallel-curl-execution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Free Geolocation API tool</title>
		<link>http://www.codediesel.com/tools/free-geolocation-api-tool/</link>
		<comments>http://www.codediesel.com/tools/free-geolocation-api-tool/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 04:33:15 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[data]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=682</guid>
		<description><![CDATA[NOTE: http://www.ip2location.com have done away with the free api access from their site. So the following sample code will no longer work. They now provide a free sample database on their site and also a complete paid version. iplocationtools.com offers a free geolocation API that lets you query with an ip address and get the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>NOTE: http://www.ip2location.com have done away with the free api access from their site. So the following sample code will no longer work. They now provide a free sample database on their site and also a complete paid version.</strong><br />
<br />
<del datetime="2009-07-15T16:28:16+00:00"><a target="_blank" href="http://iplocationtools.com/">iplocationtools.com</a>  offers a free geolocation API that lets you query with an ip address and get the location details such as city, country, zip, latitude, longitude etc. The site also offers a free MySQL database for the same if you would like to install it on your server. I&#8217;ve used CURL to wrap the API access. The complete function with a sample query and the response is shown below.</del><br />
<span id="more-682"></span></p>

<div class="wp_codebox"><table><tr id="p68213"><td class="code" id="p682code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
   <span style="color: #009933; font-style: italic;">/**
    * Geolocation API access
    *
    * @param    string  $ip         IP address to query
    * @param    string  $format     output format of response
    *
    * @return   string  XML, JSON or CSV string
    */</span>
    <span style="color: #000000; font-weight: bold;">function</span> get_ip_location<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> <span style="color: #000088;">$format</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;xml&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* Set allowed output formats */</span>
        <span style="color: #000088;">$formats_allowed</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;json&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;xml&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;raw&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* IP location query url */</span>
        <span style="color: #000088;">$query_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://iplocationtools.com/ip_query.php?ip=&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* Male sure that the format is one of json, xml, raw.
           Or else default to xml */</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$format</span><span style="color: #339933;">,</span> <span style="color: #000088;">$formats_allowed</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$format</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xml&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$query_url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query_url</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$ip}</span>&amp;output=<span style="color: #006699; font-weight: bold;">{$format}</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* Init CURL and its options*/</span>
        <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$query_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* Execute CURL and get the response */</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000088;">$location_data</span> <span style="color: #339933;">=</span> get_ip_location<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;209.85.153.104&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$location_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Query response:</p>

<div class="wp_codebox"><table><tr id="p68214"><td class="code" id="p682code14"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Response<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Ip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>209.85.153.104<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Ip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Status<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>OK<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Status<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CountryCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>US<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CountryCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CountryName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>United States<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CountryName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RegionCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>06<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/RegionCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RegionName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>California<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/RegionName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;City<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Mountain View<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/City<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ZipPostalCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>94043<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ZipPostalCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>37.4192<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-122.057<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Response<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The function by default returns a XML response, but you can also get a response in json or csv format, as shown below.</p>

<div class="wp_codebox"><table><tr id="p68215"><td class="code" id="p682code15"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Get response in 'json' format' */</span>
<span style="color: #000088;">$location_data</span> <span style="color: #339933;">=</span> get_ip_location<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;209.85.153.104&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p68216"><td class="code" id="p682code16"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
<span style="color: #3366CC;">&quot;Ip&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;209.85.153.104&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;Status&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;OK&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;CountryCode&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;US&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;CountryName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;United States&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;RegionCode&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;06&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;RegionName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;California&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;City&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Mountain View&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;ZipPostalCode&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;94043&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;Latitude&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;37.4192&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;Longitude&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;-122.057&quot;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>or</p>

<div class="wp_codebox"><table><tr id="p68217"><td class="code" id="p682code17"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Get response in 'csv' format' */</span>
<span style="color: #000088;">$location_data</span> <span style="color: #339933;">=</span> get_ip_location<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;209.85.153.104&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;raw&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p68218"><td class="code" id="p682code18"><pre class="csv" style="font-family:monospace;">209.85.153.104,OK,US,United States,06,California,
Mountain View,94043,37.4192,-122.057</pre></td></tr></table></div>

<p>You can further parse the returned XML using SimpleXML.</p>

<div class="wp_codebox"><table><tr id="p68219"><td class="code" id="p682code19"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000088;">$location_data</span> <span style="color: #339933;">=</span> get_ip_location<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;209.85.153.104&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
    try <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$location_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Error parsing XML.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/free-geolocation-api-tool/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>6 essential cURL commands for daily use</title>
		<link>http://www.codediesel.com/tools/6-essential-curl-commands/</link>
		<comments>http://www.codediesel.com/tools/6-essential-curl-commands/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 08:35:30 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[curl]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=538</guid>
		<description><![CDATA[cURL is a command line tool for doing all sorts of interesting and essential URL manipulations and data transfers. The original goal of the program was to transfer files programmatically via protocols such as http, ftp, gopher, sftp, ftps, scp, tftp, and many others, via a command line interface. The main benefit of using the [...]]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://curl.haxx.se/download.html">cURL</a> is a command line tool for doing all sorts of interesting and essential URL manipulations and data transfers. The original goal of the program was to transfer files programmatically via protocols such as http, ftp, gopher, sftp, ftps, scp, tftp, and many others, via a command line interface. The main benefit of using the command line interface is that you can use the program in your Windows batch file or Linux shell scripts to automate many URL related processes. In this post you will see some essential things you can do using cURL.<br />
<span id="more-538"></span><br />
<strong>1. Reading URLs</strong></p>
<p>Read a plain URL.</p>

<div class="wp_codebox"><table><tr id="p53838"><td class="code" id="p538code38"><pre class="dos" style="font-family:monospace;">curl http://www.google.com</pre></td></tr></table></div>

<p>Read a secured URL.</p>

<div class="wp_codebox"><table><tr id="p53839"><td class="code" id="p538code39"><pre class="dos" style="font-family:monospace;">curl https://www.secure-site.com</pre></td></tr></table></div>

<p>Get a web page and store it in a file. The following for example will store the index page retrieved to the file savedpage.html</p>

<div class="wp_codebox"><table><tr id="p53840"><td class="code" id="p538code40"><pre class="dos" style="font-family:monospace;">curl -o savedpage.html http://www.example.com/</pre></td></tr></table></div>

<p>Get a HTTP Basic authenticated page</p>

<div class="wp_codebox"><table><tr id="p53841"><td class="code" id="p538code41"><pre class="dos" style="font-family:monospace;">curl -u username:password http://www.example.com/</pre></td></tr></table></div>

<p>Sometimes a page may redirect to another resource. By default CURL will not follow page redirections. To make CURL follow redirections use the -L option.</p>

<div class="wp_codebox"><table><tr id="p53842"><td class="code" id="p538code42"><pre class="dos" style="font-family:monospace;">curl -L http://www.example.com/</pre></td></tr></table></div>

<p><strong>2. Reading URL&#8217;s with variable GET parameters</strong></p>
<p>You can also download pages with a variable GET parameter. For e.g take the following url:</p>

<div class="wp_codebox"><table><tr id="p53843"><td class="code" id="p538code43"><pre class="dos" style="font-family:monospace;">http://example.com/pages.php?pageNo=<span style="color: #cc66cc;">35</span></pre></td></tr></table></div>

<p>The variable here is the pageNo parameter. You can download all the pages by adding a regular expression like parameter in the CURL url as given below.</p>

<div class="wp_codebox"><table><tr id="p53844"><td class="code" id="p538code44"><pre class="dos" style="font-family:monospace;">curl -o pages#<span style="color: #cc66cc;">1</span>.html http://example.com/pages.php?pageNo=<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>-<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p>This will download all the pages from page no 1 to page no 12 and save it to a corresponding file.</p>
<p><strong>3. Reading document information</strong></p>
<p>Show document headers only</p>

<div class="wp_codebox"><table><tr id="p53845"><td class="code" id="p538code45"><pre class="dos" style="font-family:monospace;">curl --head http://www.google.com/</pre></td></tr></table></div>

<p>You can also use it on any specific resource.</p>

<div class="wp_codebox"><table><tr id="p53846"><td class="code" id="p538code46"><pre class="dos" style="font-family:monospace;">curl --head http://www.google.com/logo_plain.jpg</pre></td></tr></table></div>

<p>Dump document headers to a file</p>

<div class="wp_codebox"><table><tr id="p53847"><td class="code" id="p538code47"><pre class="dos" style="font-family:monospace;">curl --dump-header headers.txt http://www.google.com/</pre></td></tr></table></div>

<p><strong>4. CURL and FTP</strong></p>
<p>Get a FTP directory listing</p>

<div class="wp_codebox"><table><tr id="p53848"><td class="code" id="p538code48"><pre class="dos" style="font-family:monospace;">curl ftp://username:password<span style="color: #33cc33;">@</span>example.com</pre></td></tr></table></div>

<p>To get the listing of a different directory append the directory name to the URL.</p>

<div class="wp_codebox"><table><tr id="p53849"><td class="code" id="p538code49"><pre class="dos" style="font-family:monospace;">curl ftp://username:password<span style="color: #33cc33;">@</span>example.com/directory/</pre></td></tr></table></div>

<p>Upload a file to a remote directory using FTP</p>

<div class="wp_codebox"><table><tr id="p53850"><td class="code" id="p538code50"><pre class="dos" style="font-family:monospace;">curl -T uploadfilename -u username:password ftp://sitename.com/myfile</pre></td></tr></table></div>

<p>The &#8216;uploadfilename&#8217; file will be copied to the remote site and named &#8216;myfile&#8217;. If the destination filename is eliminated the file will be copied with the original name. By default the file will be copied to the root directory. To copy to some other directory specify the directory after the site name;e.g.</p>

<div class="wp_codebox"><table><tr id="p53851"><td class="code" id="p538code51"><pre class="dos" style="font-family:monospace;">curl -T uploadfilename -u username:password 
          ftp://sitename.com/directory/myfile</pre></td></tr></table></div>

<p><strong>5. To POST to a page.</strong></p>
<p>You can also process a POST request using CURL. The data will use the application/x-www-form-urlencoded encoding. Lets say you have the following POST form in your page:</p>

<div class="wp_codebox"><table><tr id="p53852"><td class="code" id="p538code52"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;POST&quot; action=&quot;process.php&quot;&gt;
          &lt;input type=text name=&quot;item&quot;&gt;
          &lt;input type=text name=&quot;category&quot;&gt;
          &lt;input type=submit name=&quot;submit&quot; value=&quot;ok&quot;&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>You can use the following CURL command to POST the request.</p>

<div class="wp_codebox"><table><tr id="p53853"><td class="code" id="p538code53"><pre class="dos" style="font-family:monospace;">curl -d &quot;item=bottle&amp;category=consumer&amp;submit=ok&quot; 
           www.example.com/process.php</pre></td></tr></table></div>

<p><strong>6. Referer &#038; User Agent</strong></p>
<p>HTTP requests may include a &#8216;referer&#8217; field, which is used to tell from which URL the client got to this particular page. Many programs/scripts check the referer field of requests to check the source of the request. You can simulate the referer field by the following command.</p>

<div class="wp_codebox"><table><tr id="p53854"><td class="code" id="p538code54"><pre class="dos" style="font-family:monospace;"> curl -e http://some_referring_site.com  http://www.example.com/</pre></td></tr></table></div>

<p>All HTTP requests may set the User-Agent field. It names what user agent or client that is being used. Many web applications use this information to decide how to display web pages or use it to track browser usage. You can impersonate a particular browser by the following method:</p>

<div class="wp_codebox"><table><tr id="p53855"><td class="code" id="p538code55"><pre class="dos" style="font-family:monospace;">curl -A &quot;Mozilla/<span style="color: #cc66cc;">5.0</span> <span style="color: #66cc66;">&#40;</span>compatible; MSIE <span style="color: #cc66cc;">7.01</span>; Windows NT <span style="color: #cc66cc;">5.0</span><span style="color: #66cc66;">&#41;</span>&quot; 
        http://www.example.com</pre></td></tr></table></div>

<p>There are many more options you can use with curl, the ones given above are just some you may require on a regular basis.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/6-essential-curl-commands/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

