Making a PHP CURL request to HTTPS url


Recently I had been trying to make a PHP CURL request to an HTTPS url for fetching the google analytics data over their secure server url. But all I was able to get was an nasty error.

“Curl error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”

I tried many way arounds to fix the problem until I found one solution which worked for me. All you need to do is download this cacert.pem file and specify the path through the CURLOPT_CAINFO option.

This is the way you do it.

curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem")

Save the downloaded file in a directory and specify the full path to cacert.pem file.

Important, Leave all other https related CURL options (eg. CURLOPT_SSL_VERIFYPEER) to default as you would have been doing for a non-https url.

Here is full snippet that makes an HTTPS request to google analytics API to get authentication token.

 
function auth(){
 
	global $ga_username;
	global $ga_password;
 
 
	$gaUrl = "https://www.google.com/accounts/ClientLogin";
	//	
 
	$authData = "accountType=GOOGLE&Email=". $ga_username ."&service=analytics&source=My app&Passwd=". $ga_password;
 
	// create a new cURL resource
	$ch = curl_init();
 
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_URL, $gaUrl);
	curl_setopt($ch, CURLOPT_HEADER, 0);
 
	curl_setopt($ch, CURLOPT_POST, 1);
 
	curl_setopt($ch, CURLOPT_POSTFIELDS, $authData);
 
	curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
 
	$fileHandle = fopen(dirname(__FILE__) . "/error.txt","w+");
 
	curl_setopt($ch, CURLOPT_VERBOSE, true);
 
	curl_setopt($ch, CURLOPT_STDERR, $fileHandle);
 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
	// grab URL and pass it to the browser
	$response = curl_exec($ch);
 
	if(curl_exec($ch) === false)
	{
		echo 'Curl error: ' . curl_error($ch);
	}
	else
	{
		$tokens = explode("\n", trim($response));
 
		print_r($tokens);
 
	}
 
	// close cURL resource, and free up system resources
	curl_close($ch);
 
}

About Penuel

Hi I am Penuel, and I am from India. I am working as an independent web consultant in open source technologies. I love programming, trying out new things and sharing.
This entry was posted in PHP Programming, Web Development and tagged , , , . Bookmark the permalink.

2 Responses to Making a PHP CURL request to HTTPS url

  1. jimmy loan says:

    I was wondering whether SMTP connection can be done using PHP cURL function?

  2. Penuel says:

    From PHP Manual “PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication”

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">