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 = "";
	$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);
		$tokens = explode("\n", trim($response));
	// close cURL resource, and free up system resources
  • jimmy loan

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

  • Penuel

    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”

  • new exception

    also you can check if both ends use same ssl version

  • billiegod

    Solved my problem!

    I’ve been experiencing the same problem. Was using a certain commercial API to send SMS alerts to my service subscribers, and it all worked fine when I run the PHP script via the browser. It broke when I tried running it using the command line as a cron. Same cerificate error.

    So I downloaded the CA certificate file and now the cron is working like a charm! Thank you!

  • Penuel

    Cheers 🙂

  • Romain Jolivot


    that doesn’t work for me !

    I’ve got the error :

    SSL certificate problem: self signed certificate in certificate chain

    This code just worked 2 weeks ago but they have add a certificat and now it’s down

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POSTFIELDS , $json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, ‘https://www.*******.com/r/getEntities’);
    curl_setopt($ch, CURLOPT_CAINFO, $crtpath.$crtname);
    $data = curl_exec($ch);