<?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>Binary Tides &#187; CURL</title>
	<atom:link href="http://www.binarytides.com/blog/category/curl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.binarytides.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 24 Jul 2010 05:31:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Remote Login with Curl PHP</title>
		<link>http://www.binarytides.com/blog/remote-login-with-curl-php/</link>
		<comments>http://www.binarytides.com/blog/remote-login-with-curl-php/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 12:45:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[CURL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=29</guid>
		<description><![CDATA[The Curl Library of PHP can be used to open remote web pages and also logging in where a login is required.
Code

$login_url = 'http://www.somesite.com/login.php';
#These are the post data username and password
$post_data = 'username=someusername&#38;password=somepassword';
#Create a curl object
$ch = curl_init();
#Set the useragent
$agent = $_SERVER[&#34;HTTP_USER_AGENT&#34;];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
#Set the URL
curl_setopt($ch, CURLOPT_URL, $login_url );
#This is a POST query
curl_setopt($ch, CURLOPT_POST, 1 [...]]]></description>
			<content:encoded><![CDATA[<p>The Curl Library of PHP can be used to open remote web pages and also logging in where a login is required.</p>
<p><strong>Code</strong></p>
<pre class="brush: php;">
$login_url = 'http://www.somesite.com/login.php';
#These are the post data username and password
$post_data = 'username=someusername&amp;password=somepassword';
#Create a curl object
$ch = curl_init();
#Set the useragent
$agent = $_SERVER[&quot;HTTP_USER_AGENT&quot;];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
#Set the URL
curl_setopt($ch, CURLOPT_URL, $login_url );
#This is a POST query
curl_setopt($ch, CURLOPT_POST, 1 );
#Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
#We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#Follow Location redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
#Execute the action to login
$postResult = curl_exec($ch);
</pre>
<p><span id="more-29"></span></p>
<p>After the curl_exec the login page is opened with login details or simply the login has been done. Now any other url (which is login restricted) can be opened by</p>
<pre class="brush: php;">
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
</pre>
<p>The option CURLOPT_COOKIEFILE is used to specify a file which stores the cookie data that is received during the process of opening and logging into webpages. Make sure the directory is writeable by php so that it can create these files.</p>
<p>The login page of the particular site where the username and password are asked for will give various information regarding the name of fields to be send as username , password , the type of query that is POST , the form submission url to which the values are to be submitted etc. Those information should be used in the above code to specify the login url , username and password field names and values etc.</p>
<p>The same should work with ssl or https urls provided the openssl module is enabled.</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=29&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/remote-login-with-curl-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
