Unable to connect to MySQL from localhost: Strange error

If you trying to access your local MySQL database from your PHP script running on your local web server (like Apache) and encounter an error like below:

Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in D:\projects\Pinlocal\public\test.php on line 12

Warning: mysql_connect() [function.mysql-connect]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in D:\projects\Pinlocal\public\test.php on line 12

Chances are that your MySQL database has been blocked by a firewall. However in my case my Firewall (McAfee Firewall) was configured to allow MySQL connections. I researched on net and all seem to point to configuring Firewall to allow MySQL. After about an hour of research I was stuck on the same problem and apparently the problem was something different since MySQL was already in my firewall exception list.

Few days back my McAfee alerted me that my Apache  Server is attempting to make a connection and I instructed it to block the connection. I opened the Firewall console and found that my local Apache Web server has been blocked from making any type of connection. I unblocked my Apache executable and allowed it to make Outgoing connections and to my delight I am now able to connect to MySQL from my php script without any problems. Clearly my McAfee firewall was not allowing Apache to make a connection to MySQL and the above error message was somewhat misleading. I documented it here so may be it would help someone out there facing the same problem, as I am sure uninstalling and installing MySQL wasn’t going to solve the problem which I was thinking to do as last resort.

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