<?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>My Scribbles &#187; php curl example</title>
	<atom:link href="http://www.blazecoder.com/tag/php-curl-example/feed" rel="self" type="application/rss+xml" />
	<link>http://www.blazecoder.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Jul 2010 05:15:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using cURL with PHP</title>
		<link>http://www.blazecoder.com/php-programming/using-curl-with-php</link>
		<comments>http://www.blazecoder.com/php-programming/using-curl-with-php#comments</comments>
		<pubDate>Fri, 16 Oct 2009 23:42:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php programming]]></category>
		<category><![CDATA[curl syntax in php]]></category>
		<category><![CDATA[domdocument loadxml]]></category>
		<category><![CDATA[How to use curl in php]]></category>
		<category><![CDATA[php curl example]]></category>
		<category><![CDATA[php curl tutorial]]></category>
		<category><![CDATA[php domdocument]]></category>
		<category><![CDATA[php domdocument loadxml syntax]]></category>
		<category><![CDATA[xml domdocument]]></category>

		<guid isPermaLink="false">http://localhost/suarezbevs/?p=10</guid>
		<description><![CDATA[Today,  I&#8217;ll give you some hint about how to use cURL in PHP.
cURL is a library that allow a webserver to transfer files with a remote computer using a variety of Internet protocols. cURL stands for &#8220;Client URLs&#8221;, it is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, [...]]]></description>
			<content:encoded><![CDATA[<p>Today,  I&#8217;ll give you some hint about how to use cURL in PHP.</p>
<p>cURL is a library that allow a webserver to transfer files with a remote computer using a variety of Internet protocols. cURL stands for &#8220;Client URLs&#8221;, it is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, HTTP, HTTPS,GOPHER, LDAP, DICT, TELNET and FILE.</p>
<p>Once you&#8217;ve compiled PHP with cURL support, you can begin using the cURL functions.</p>
<p>The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(),  then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close().</p>
<p>Using cURL from the command line is easy.  Here is an example that uses PHP cURL functions to fetch the data from <a href="http://www.poweropt.com/optionable.asp?fl=A">PowerOptions</a> Website  and prints it&#8217;s output. The data that I&#8217;m going to harvest would be the Company Name and Symbol.</p>
<p>Here is the function  I&#8217;ve used:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> scrape<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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>
&nbsp;
<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_USERAGENT<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10&quot;</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_FAILONERROR<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: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<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: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_AUTOREFERER<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: #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: #009900; font-weight: bold;">true</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_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</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_VERBOSE<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: #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: #0000ff;">&quot;http://www.poweropt.com/optionable.asp?fl=A&quot;</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;">120</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_PROXY<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;192.168.20.206:3128&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$html</span><span style="color: #339933;">=</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>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;cURL error number:&quot;</span> <span style="color: #339933;">.</span><span style="color: #990000;">curl_errno</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</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>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;cURL error:&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">curl_error</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</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>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">curl_close</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: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #000088;">$dom</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>loadHTML<span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nodeLists</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>evaluate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;//*[contains(text(), 'Company Name')]/ancestor::table[1]//tr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$nodeLists</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>length<span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$nodeLists</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>firstChild<span style="color: #339933;">;</span>
	<span style="color: #000088;">$tempNode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>firstChild<span style="color: #339933;">;</span>
	<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tempNode</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nodeValue<span style="color: #339933;">;</span>
	<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nextSibling<span style="color: #339933;">;</span>
	<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nextSibling<span style="color: #339933;">;</span>
	<span style="color: #000088;">$symbol</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nodeValue<span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//echo $name.&quot; | &quot;. $symbol .&quot;&quot;;</span>
	<span style="color: #666666; font-style: italic;">//echo &quot;&lt;span style=&quot;font-family: Tahoma; color: black;&quot;&gt;&quot;.$name. &quot;|&quot;. $symbol .&quot;&lt;/span&gt;&quot;;</span>
	<span style="color: #666666; font-style: italic;">//Saving the data autpmaticaly after scraping.</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO company (company_name, symbol) VALUES('<span style="color: #006699; font-weight: bold;">$name</span>','<span style="color: #006699; font-weight: bold;">$symbol</span>')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the above example,<strong> curl_exec($ch)</strong> execute the returned data from the remote server and stored in the variable $html.</p>
<p>For more advance technique,  I used  <strong>PHP DOMDocument() Class</strong> for extracting the harvested data instead of using php <strong>preg_match_all</strong> function.</p>
<p>Source:  <a href="http://www.php.net/manual/en/curl.examples-basic.php">php.net</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.blazecoder.com/php-programming/using-curl-with-php/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
