<?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>BLOGS@DiGiTSS &#187; cURL</title>
	<atom:link href="http://blogs.digitss.com/category/php/curl-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.digitss.com</link>
	<description>DiGiTSS Team&#039;s Programming experience with PHP, MySQL, Ajax, Javascript, jQuery, C# and Microsoft technologies</description>
	<lastBuildDate>Sat, 13 Aug 2011 06:26:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Posting or Uploading Files using cURL with PHP</title>
		<link>http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/</link>
		<comments>http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 18:40:33 +0000</pubDate>
		<dc:creator>Dharmavirsinh Jhala</dc:creator>
				<category><![CDATA[cURL]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Web Developer]]></category>

		<guid isPermaLink="false">http://blogs.digitss.com/?p=561</guid>
		<description><![CDATA[cURL is very important tool for PHP programmers, we tends to do so many thing with using this tool. Posting data, imitating a form post and lot more, it's usage is countless. Some time back I came across situation where we have to post files to a 3rd party faxing service. I knew there are many possibilities but I didn't wanted to get into sockets and file stream to get this done, just wanted to post FILE in addition to rest of the post data.]]></description>
			<content:encoded><![CDATA[<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<div id="attachment_563" class="wp-caption alignleft" style="width: 109px"><a href="http://blogs.digitss.com/wp-content/uploads/2011/03/curl.png"><img class="size-full wp-image-563" title="cURL - groks those URLs" src="http://blogs.digitss.com/wp-content/uploads/2011/03/curl.png" alt="cURL - groks those URLs" width="99" height="37" /></a><p class="wp-caption-text">groks URLs</p></div>
<p></strong></p>
<p><strong> </strong></p>
<p><strong>cURL</strong> is very important tool for <strong>PHP</strong> programmers, we tends to do so many thing with using this tool. Posting data, imitating a form post and lot more, it's usage is countless. Some time back I came across situation where we have to post files to a 3rd party faxing service. I knew there are many possibilities but I didn't wanted to get into sockets and file stream to get this done, just wanted to post FILE in addition to rest of the post data.</p>
<pre class="php"><span style="color: #808080; font-style: italic;">// URL on which we have to post data</span>
<span style="color: #0000ff;">$url</span> = <span style="color: #ff0000;">&quot;http://localhost/tutorials/post_action.php&quot;</span>;
<span style="color: #808080; font-style: italic;">// Any other field you might want to catch</span>
<span style="color: #0000ff;">$post_data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;khan&quot;</span>;
<span style="color: #808080; font-style: italic;">// File you want to upload/post</span>
<span style="color: #0000ff;">$post_data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'file'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;@c:/logs.log&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Initialize cURL</span>
<span style="color: #0000ff;">$ch</span> = curl_init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Set URL on which you want to post the Form and/or data</span>
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_URL, <span style="color: #0000ff;">$url</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Data+Files to be posted</span>
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_POSTFIELDS, <span style="color: #0000ff;">$post_data</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Pass TRUE or 1 if you want to wait for and catch the response against the request made</span>
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// For Debug mode; shows up any error encountered during the operation</span>
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_VERBOSE, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Execute the request</span>
<span style="color: #0000ff;">$response</span> = curl_exec<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Just for debug: to see response</span>
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$response</span>;</pre>
<p>Above Code is the bare minimum required to post/upload file using CURL. As used in above example, it's not mandatory to use array index as "<strong>file</strong>". You can use anything required depending upon the requirement.</p>
<p>Posting multiple files is also easy, with the same code you can add multiple file upload using following lines:</p>
<pre class="php"><span style="color: #0000ff;">$post_data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'alian_error_log'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;@c:/alian_app_error.log&quot;</span>;
<span style="color: #0000ff;">$post_data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'myapp_error_log'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;@c:/myapp_error.log&quot;</span>;</pre>
<p>Here <strong>@</strong> is the key character, prefixing local file path with <strong>@</strong> does all the trick.</p>
<p>﻿<span id="more-561"></span></p>
<p><div id="attachment_562" class="wp-caption alignleft" style="width: 331px"><a href="http://blogs.digitss.com/wp-content/uploads/2011/03/curl_post_result.png"><img class="size-full wp-image-562" title="cURL Post - as being received on posted URL" src="http://blogs.digitss.com/wp-content/uploads/2011/03/curl_post_result.png" alt="cURL Post - as being received on posted URL" width="321" height="308" /></a><p class="wp-caption-text">cURL Post - as being received on posted URL</p></div><br />
<!--adsense--></p>
<div style="clear:both">
For newbies cURL as it's described on <a title="cURL Project Home" href="http://curl.haxx.se/" target="_blank"><strong>cURL home</strong></a>:</p>
<blockquote><p>curl is a command line tool for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks.</p></blockquote>
</div>
<p>With <strong>PHP</strong> we can use <strong>cURL</strong> using <a title="libcurl for PHP" href="http://curl.haxx.se/libcurl/php/" target="_blank">libcurl extension for PHP</a>. It comes bundled with most of the <a title="Setting up PHP, MySQL, Apache with most up-to-date WAMP Package" href="http://blogs.digitss.com/php/setting-up-php-mysql-apache-with-most-up-to-date-wamp-package/">WAMP packages</a> and available on almost all managed/web hosting environments.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://blogs.digitss.com/php/php-downloading-a-file-from-secure-website-https-using-curl/" rel="bookmark" title="October 25, 2008">PHP &#8211; Downloading a File from Secure website (https) using CURL</a></li>
<li><a href="http://blogs.digitss.com/database/mysql/export-import-csv-files-with-mysql-no-external-tool-required/" rel="bookmark" title="November 14, 2009">Export/Import CSV files with MySQL &#8211; No external tool required</a></li>
<li><a href="http://blogs.digitss.com/javascript/simple-plain-ajax-without-any-javascript-library/" rel="bookmark" title="April 22, 2011">Simple / Plain Ajax &#8211; without any JavaScript library</a></li>
<li><a href="http://blogs.digitss.com/php/truncate-last-n-lines-of-a-file-using-php/" rel="bookmark" title="April 23, 2011">Truncate last N lines of a file using PHP</a></li>
<li><a href="http://blogs.digitss.com/programming/ag_e_parser_bad_property_value/" rel="bookmark" title="November 13, 2009">AG_E_PARSER_BAD_PROPERTY_VALUE</a></li>
</ul>
<p><!-- Similar Posts took 223.775 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Downloading a File from Secure website (https) using CURL</title>
		<link>http://blogs.digitss.com/php/php-downloading-a-file-from-secure-website-https-using-curl/</link>
		<comments>http://blogs.digitss.com/php/php-downloading-a-file-from-secure-website-https-using-curl/#comments</comments>
		<pubDate>Sat, 25 Oct 2008 17:43:03 +0000</pubDate>
		<dc:creator>Dharmavirsinh Jhala</dc:creator>
				<category><![CDATA[cURL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tools / Plugins / Extenstion]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[secure file download]]></category>

		<guid isPermaLink="false">http://blogs.digitss.com/?p=132</guid>
		<description><![CDATA[Recently in one of my project I was suppose to download file from a secure website. Actually I have to set the scheduler job for downloading nightly builds/full files from the server and the files are placed on secure website.]]></description>
			<content:encoded><![CDATA[<p>Recently in one of my project I was suppose to download file from a secure website. Actually I have to set the scheduler job for downloading nightly builds/full files from the server and the files are placed on secure website.</p>
<p>Initially I tried that with file_get_contents function but it was just downloading 0KB file. Then it strike me that oh..! I can't use <strong>file_get_contents</strong> as it can't understand the HTTPS protocol here. So what could help me nothing else then our best friend <strong>CURL</strong>. <strong>file_get_contents</strong> and <strong>fopen</strong> kind of other functions works fine with any <strong>http</strong> web locations.</p>
<p>Here is the simple CURL file transfer function snippet which will copy file from any secure http location.<span id="more-132"></span><!--adsense--></p>
<pre class="php">	<span style="color: #808080; font-style: italic;">/**
	 * Copy File from HTTPS/SSL location
	 *
	 * @param string $FromLocation
	 * @param string $ToLocation
	 * @return boolean
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> copySecureFile<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$FromLocation</span>,<span style="color: #0000ff;">$ToLocation</span>,<span style="color: #0000ff;">$VerifyPeer</span>=<span style="color: #000000; font-weight: bold;">false</span>,<span style="color: #0000ff;">$VerifyHost</span>=<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// Initialize CURL with providing full https URL of the file location</span>
		<span style="color: #0000ff;">$Channel</span> = curl_init<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$FromLocation</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Open file handle at the location you want to copy the file: destination path at local drive</span>
		<span style="color: #0000ff;">$File</span> = <a href="http://www.php.net/fopen"><span style="color: #000066;">fopen</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ToLocation</span>, <span style="color: #ff0000;">&quot;w&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Set CURL options</span>
		curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Channel</span>, CURLOPT_FILE, <span style="color: #0000ff;">$File</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// We are not sending any headers</span>
		curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Channel</span>, CURLOPT_HEADER, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Disable PEER SSL Verification: If you are not running with SSL or if you don't have valid SSL</span>
		curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Channel</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: #0000ff;">$VerifyPeer</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Disable HOST (the site you are sending request to) SSL Verification,</span>
		<span style="color: #808080; font-style: italic;">// if Host can have certificate which is nvalid / expired / not signed by authorized CA.</span>
		curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Channel</span>, CURLOPT_SSL_VERIFYHOST, <span style="color: #0000ff;">$VerifyHost</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Execute CURL command</span>
		curl_exec<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Channel</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Close the CURL channel</span>
		curl_close<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Channel</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Close file handle</span>
		<a href="http://www.php.net/fclose"><span style="color: #000066;">fclose</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$File</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// return true if file download is successfull</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/file_exists"><span style="color: #000066;">file_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ToLocation</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">/**
	 *	This code don't work
	 *	echo file_get_contents(&quot;https://www.verisign.com/hp07/i/vlogo.gif&quot;);
	**/</span>
	<span style="color: #808080; font-style: italic;">// Function Usage</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>copySecureFile<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;https://www.verisign.com/hp07/i/vlogo.gif&quot;</span>,<span style="color: #ff0000;">&quot;c:/verisign_logo.gif&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'File transferred successfully.'</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #66cc66;">&#123;</span>
		<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'File transfer failed.'</span>;
	<span style="color: #66cc66;">&#125;</span></pre>
<p><!--adsense--><br />
Please let me know if there are any other ways of doing that; other than this work around.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/" rel="bookmark" title="March 6, 2011">Posting or Uploading Files using cURL with PHP</a></li>
<li><a href="http://blogs.digitss.com/apache/allow-only-https-access-with-htaccess/" rel="bookmark" title="April 26, 2009">Allow only HTTPS access with .htaccess</a></li>
<li><a href="http://blogs.digitss.com/programming/printing-data-table-vertically-why-and-how/" rel="bookmark" title="January 3, 2011">Printing data table vertically..! Why? and How?</a></li>
<li><a href="http://blogs.digitss.com/php/truncate-last-n-lines-of-a-file-using-php/" rel="bookmark" title="April 23, 2011">Truncate last N lines of a file using PHP</a></li>
<li><a href="http://blogs.digitss.com/php/php-performance-improvement-tips/" rel="bookmark" title="August 9, 2008">PHP: Performance Improvement Tips</a></li>
</ul>
<p><!-- Similar Posts took 79.151 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.digitss.com/php/php-downloading-a-file-from-secure-website-https-using-curl/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.399 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2011-08-17 19:20:18 -->

