<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Magp.ie &#187; cURL</title>
	<atom:link href="http://magp.ie/tag/curl/feed/" rel="self" type="application/rss+xml" />
	<link>http://magp.ie</link>
	<description>A nest for the random, shiny, online tidbits I stumble across...</description>
	<lastBuildDate>Tue, 31 Jan 2012 19:01:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='magp.ie' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/061e340c5da13b5a41ae8016bee03aa8?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Magp.ie &#187; cURL</title>
		<link>http://magp.ie</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://magp.ie/osd.xml" title="Magp.ie" />
	<atom:link rel='hub' href='http://magp.ie/?pushpress=hub'/>
		<item>
		<title>Post XML with HTTPS Authentication using PHP and cURL</title>
		<link>http://magp.ie/2010/04/12/post-xml-with-https-authentication-using-php-curl/</link>
		<comments>http://magp.ie/2010/04/12/post-xml-with-https-authentication-using-php-curl/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 19:56:44 +0000</pubDate>
		<dc:creator>Eoin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[HTTPS Authentication]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://magp.ie/?p=175</guid>
		<description><![CDATA[Recently I needed to post XML to a particular API and this API required HTTPS authentication. So I needed to send a username / password with the request. This was pretty straightforward in C#.NET ( at least from my foggy &#8230; <a href="http://magp.ie/2010/04/12/post-xml-with-https-authentication-using-php-curl/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&amp;blog=11708208&amp;post=175&amp;subd=blogalhost&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I needed to post XML to a particular API and this API required HTTPS authentication. So I needed to send a username / password with the request. </p>
<p>This was pretty straightforward in C#.NET ( at least from my foggy memory ). You needed to use <a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.net.networkcredential.username.aspx">NetworkCredentials</a>. But it was a bit difficult to find a similar way of doing this in PHP&#8230; but <i>eventually</i> I found this <a target="_blank" href="http://blog.taragana.com/index.php/archive/how-to-use-curl-in-php-for-authentication-and-ssl-communication/">gem</a>.</p>
<p>This uses <a target="_blank" href="http://www.php.net/manual/en/book.curl.php">cURL</a> which is just a library to send and receive data from remote sites. Now all I needed was a neat way of using cURL to post XML&#8230; and I found a neat implementation <a target="_blank" href="http://forums.digitalpoint.com/showthread.php?t=424619&amp;s=d790ff51b6a3b7b6262f37c396a06b9c#post4004636">here</a>.</p>
<p>Two plus two and you get &#8230;<br />
<span id="more-175"></span><br />
<pre class="brush: php;">
$request_xml = &quot;&lt;?xml version='1.0' encoding='utf-8'?&gt;
&lt;request&gt;
	&lt;id&gt;12345&lt;/id&gt;
	&lt;email&gt;eoin@dolepaddy.com&lt;/email&gt;
&lt;request&gt;&quot;;

//Initialize handle and set options
$username = 'dolepaddy';
$password = 'secret123';
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://connect.tosomeserver.com/xmlrpc'); 
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_xml); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));

//Execute the request and also time the transaction ( optional )
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch); 
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;

//Check for errors ( again optional )
if ( curl_errno($ch) ) {
	$result = 'ERROR -&gt; ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
	$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
	switch($returnCode){
		case 200:
			break;
		default:
			$result = 'HTTP ERROR -&gt; ' . $returnCode;
			break;
	}
}

//Close the handle
curl_close($ch);

//Output the results and time
echo 'Total time for request: ' . $totalTime . &quot;\n&quot;;
echo $result; 
</pre></p>
<br />Filed under: <a href='http://magp.ie/category/code/'>Code</a> Tagged: <a href='http://magp.ie/tag/curl/'>cURL</a>, <a href='http://magp.ie/tag/https-authentication/'>HTTPS Authentication</a>, <a href='http://magp.ie/tag/php/'>php</a>, <a href='http://magp.ie/tag/xml/'>XML</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/blogalhost.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogalhost.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/blogalhost.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/blogalhost.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/blogalhost.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/blogalhost.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/blogalhost.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/blogalhost.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/blogalhost.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/blogalhost.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/blogalhost.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/blogalhost.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/blogalhost.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/blogalhost.wordpress.com/175/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=magp.ie&amp;blog=11708208&amp;post=175&amp;subd=blogalhost&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://magp.ie/2010/04/12/post-xml-with-https-authentication-using-php-curl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<georss:point>53.734750 -8.989992</georss:point>
		<geo:lat>53.734750</geo:lat>
		<geo:long>-8.989992</geo:long>
		<media:content url="http://1.gravatar.com/avatar/72dd449e5e79e046c1c09ed8712b525a?s=96&#38;d=monsterid&#38;r=PG" medium="image">
			<media:title type="html">eoigal</media:title>
		</media:content>
	</item>
	</channel>
</rss>
