<?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>PHP Software Developing &#187; cURL</title>
	<atom:link href="http://www.phpdeveloping.co.za/tag/curl/feed" rel="self" type="application/rss+xml" />
	<link>http://www.phpdeveloping.co.za</link>
	<description>for the love of PHP Development</description>
	<lastBuildDate>Tue, 29 Sep 2009 15:38:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Downloading PDF files</title>
		<link>http://www.phpdeveloping.co.za/download/downloading-pdf-files.html</link>
		<comments>http://www.phpdeveloping.co.za/download/downloading-pdf-files.html#comments</comments>
		<pubDate>Thu, 10 Sep 2009 05:45:47 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Download]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[pdf]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=135</guid>
		<description><![CDATA[If you&#8217;ve done the Thirty Day Challenge, you&#8217;ll know how tedious it can be to download all the PDF files.  Norio created a cool downloading script that saved me a lot of time.  I was just too lazy to sit and write something like it, so I&#8217;m stealing his!

&#60;?php
// make sure our download [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve done the <a href="http://www.thirtydaychallenge.com">Thirty Day Challenge</a>, you&#8217;ll know how tedious it can be to download all the PDF files.  <a href="http://www.norio.co.za">Norio</a> created a cool downloading script that saved me a lot of time.  I was just too lazy to sit and write something like it, so I&#8217;m stealing his!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// make sure our download doesn't time out or get interrupted by closing the browser</span>
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ignore_user_abort</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// destination to download to</span>
<span style="color: #000088;">$file_dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sites/default/files/30dc&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// create the destination directory if it doesn't exist</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// go through each day of training (1-31)</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;">&lt;=</span> <span style="color: #cc66cc;">31</span><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: #666666; font-style: italic;">// download the HTML contents of the training page for that day</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.thirtydaychallenge.com/training/2009day&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%02d</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// provide some feedback on where we are</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;Day <span style="color: #006699; font-weight: bold;">$i</span>:&lt;/b&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// flush output to browser - see php.net/flush</span>
    <span style="color: #990000;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// directory to download the current day's PDFs to</span>
    <span style="color: #000088;">$daydir</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file_dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/day<span style="color: #006699; font-weight: bold;">$i</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// create the directory if it doesn't exist</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$daydir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$daydir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// grab all the URLs to the PDFs (regular expressions are awesome!)</span>
    <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'~(http://media.thirtydaychallenge.com.s3.amazonaws.com/training09/([0-9A-Za-z_]+.pdf))~'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// go through each url we grabbed above</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// check if the file already exists (no use in re-downloading PDFs we have)</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</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: #666666; font-style: italic;">// provide some feedback on where we are</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Downloading {<span style="color: #006699; font-weight: bold;">$matches[2]</span>[<span style="color: #006699; font-weight: bold;">$key</span>]}.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// flush output to browser</span>
        <span style="color: #990000;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// download the pdf and store it locally</span>
        <span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$daydir}</span>/{<span style="color: #006699; font-weight: bold;">$matches[2]</span>[<span style="color: #006699; font-weight: bold;">$key</span>]}&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a target=_blank href="http://php.boff.co.za/quick-pdf-downloading-script-done-php">Check out the full post at Boff.co.za</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/download/downloading-pdf-files.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML Headers</title>
		<link>http://www.phpdeveloping.co.za/curl/html-headers.html</link>
		<comments>http://www.phpdeveloping.co.za/curl/html-headers.html#comments</comments>
		<pubDate>Mon, 27 Jul 2009 19:48:36 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[cURL]]></category>
		<category><![CDATA[html headers]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=71</guid>
		<description><![CDATA[In various circumstances I&#8217;ve needed to have PHP pull the information on HTML headers that a web server returns.  I&#8217;ve found cURL to provide an excellent solution to this.

  $htmlheader = &#34;&#34;;
&#160;
  function html_header&#40;$url&#41; &#123;
    global $htmlheader;
    $htmlheader = &#34;&#34;;
    $ch = curl_init&#40;&#41;;
 [...]]]></description>
			<content:encoded><![CDATA[<p>In various circumstances I&#8217;ve needed to have PHP pull the information on HTML headers that a web server returns.  I&#8217;ve found cURL to provide an excellent solution to this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$htmlheader</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> html_header<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$htmlheader</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$htmlheader</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</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;">$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_HEADERFUNCTION<span style="color: #339933;">,</span> <span style="color: #0000ff;">'readHeader'</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_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: #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: #b1b100;">return</span> <span style="color: #000088;">$htmlheader</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> readHeader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #000088;">$header</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$htmlheader</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$htmlheader</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$header</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$header</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The variable will contain something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">HTTP<span style="color: #339933;">/</span><span style="color:#800080;">1.1</span> <span style="color: #cc66cc;">200</span> OK
<span style="color: #990000;">Date</span><span style="color: #339933;">:</span> Thu<span style="color: #339933;">,</span> <span style="color: #cc66cc;">23</span> Jul <span style="color: #cc66cc;">2009</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">56</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">23</span> GMT
Server<span style="color: #339933;">:</span> Apache
X<span style="color: #339933;">-</span>Powered<span style="color: #339933;">-</span>By<span style="color: #339933;">:</span> PHP<span style="color: #339933;">/</span>5<span style="color: #339933;">.</span>2<span style="color: #339933;">.</span>0<span style="color: #339933;">-</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">+</span>etch13
Content<span style="color: #339933;">-</span>Type<span style="color: #339933;">:</span> text<span style="color: #339933;">/</span>html<span style="color: #339933;">;</span> charset<span style="color: #339933;">=</span>UTF<span style="color: #339933;">-</span><span style="color: #cc66cc;">8</span>
Via<span style="color: #339933;">:</span> <span style="color:#800080;">1.1</span> bc1<span style="color: #339933;">-</span>rba
Transfer<span style="color: #339933;">-</span>Encoding<span style="color: #339933;">:</span> chunked
Connection<span style="color: #339933;">:</span> Keep<span style="color: #339933;">-</span>Alive
Age<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span></pre></div></div>

<p>There are two functions above.  html_header is just the function to does the call to the URL and collects the header information.  The header information is actually captured by the readHeader function.  </p>
<p>You need to return the length of the header back to the CURLOPT_HEADERFUNCTION call, which is where the need for the global variable comes in.  There might be a more elegant way of doing this, perhaps rather building this function into a class of it&#8217;s own.  But I hope the above shows you how to get the information you require.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/curl/html-headers.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Faking your User Agent with cURL</title>
		<link>http://www.phpdeveloping.co.za/curl/faking-your-user-agent-with-curl.html</link>
		<comments>http://www.phpdeveloping.co.za/curl/faking-your-user-agent-with-curl.html#comments</comments>
		<pubDate>Mon, 27 Jul 2009 15:34:18 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[cURL]]></category>
		<category><![CDATA[fake user agent]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=53</guid>
		<description><![CDATA[It&#8217;s very often needed to fake your User Agent, not to do any untoward, but to test various aspects of your website.  Perhaps you want your website to display differently when using Internet Explorer 7 than when using Firefox 3.5.  For whatever reason you have, here is a simple solution I use for [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s very often needed to fake your User Agent, not to do any untoward, but to test various aspects of your website.  Perhaps you want your website to display differently when using Internet Explorer 7 than when using Firefox 3.5.  For whatever reason you have, here is a simple solution I use for my web crawlers.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$userAgent</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6&quot;</span><span style="color: #339933;">;</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_USERAGENT<span style="color: #339933;">,</span> <span style="color: #000088;">$userAgent</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;">$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_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_VERBOSE<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</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></pre></div></div>

<p>With a bit of coding you can fill up an array with all the user agent strings you can find on the Internet, and randomly use them as the user agent when using cURL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/curl/faking-your-user-agent-with-curl.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
