To log in to a HTTPS website using PHP cURL you need to do the following:
enable cURL by uncommenting the line
extension=php_curl.dll
in your php.ini file.
Set up cURL to either accept all certificates or add the needed certificate authority to cURLs CA list (check out http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/)
Then you need to load the page to get the session cookie:
// Create temp file to store cookies $ckfile = tempnam ("/tmp", "CURLCOOKIE"); // URL to login page $url = "https://www.securesiteexample.com"; // Get Login page and its cookies and save cookies in the temp file $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // Stores cookies in the temp file curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch);
Now you have the cookie, you can POST login values (check the source of the login page to check if you need any other fields too)
$fields = array( $fields = array( 'username' => 'yourusername', 'password' => 'yourpassword', ); $fields_string = ''; foreach($fields as $key=>$value) { $fields_string .= $key . '=' . $value . '&'; } rtrim($fields_string, '&'); // Post login form and follow redirects $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, count($fields)); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects $output = curl_exec($ch);Now you should be able to access any pages within the password-restricted area by just including the cookies for each call:
$url = "https://www.securesiteexample.com/loggedinpage.html"; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch);
Source from:
http://www.herikstad.net/2011/06/logging-to-https-websites-using-php.html