Home / MAMP PHP cURL and SSL

MAMP PHP cURL and SSL

I currently develop PHP based websites on a Mac using MAMP. When testing the Interspire Email Marketer API on a local version of a site posting data through to an https:// URL with cURL, I discovered that the version of cURL on MAMP has some issues with certs and SSL.

Original code

My original code posted some XML through to an https URL like so:

$ch = curl_init('https://www.example.com/example-api.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = @curl_exec($ch);

The post was failing and all that was being returned into $result was false. Running this line of code after the above:

echo '<pre>', curl_error($ch);

produced this error message:

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

My solution

I found a forum on the MAMP website which had a whole bunch of instructions about getting updated certificate files and so on, but the simplest solution was to simply not check the cert. I don’t need to have it checked because it’s a site I trust (being one of my own partner sites).

To disable the SSL checking add this line:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

The revised piece of code in its entirety now looks like this:

$ch = curl_init(IEM_APIURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = @curl_exec($ch);