Monday, August 11, 2014

cURL & SSL certificates error

This will be mainly a reminder, more than a real post.
Today I was writing a bunch of PHP bugs and suddenly a wordpress plugin of mine started to output this string :

SSL certificate problem: unable to get local issuer certificate

The plugin is quite simple itself. It just uses cURL to fetch some Facebook Pages data using the Graph urlon a Wordpress website I'm creating. The error log did not show anything about this error.



After digging the Google and PHP website I came with a simple solution (the error description was quite explanatory). The problem was I was fetching the data using the default settings for cURL, which includes the parameter :

CURLOPT_SSL_VERIFYPEER


This parameter has to be set to FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.

So the simple solution was to add the following line to my cURL setup :

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

So the final code to fetch data from the Facebook Graph is :

curl_setopt($ch,CURLOPT_URL,'https://graph.facebook.com/'.$fbpage_id);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);


No comments:

Post a Comment