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);
 
}

Zend Framework: Accessing Request, Response & Router Object from anywhere

In Zend Framework you can access the current request object from anywhere outside the current controller, for example inside your own base classes. To do so you obtain the singleton instance of the Front Controller Object and access the current request object registered with it.

Here is how you do it:

Zend_Controller_Front::getInstance()->getRequest();

Once you have the access to the Request object, you may access all other methods and objects registered to it. For example you can get the current module name as shown below

Zend_Controller_Front::getInstance()->getRequest()->getModuleName();

Above code snippet is especially useful when you are trying to detect the current request object outside the current controller class.

Accessing the instance of the Front Controller as shown above, gives you the way to access even other objects registered with it like the Response object or the Router Obect, which can be inspected or manipulated further. The below code gives reference to the current Response object.

Zend_Controller_Front::getInstance()->getResponse();

To access the Router object you may use the following code:

Zend_Controller_Front::getInstance()->getRouter();