I've set up a plugin for a Wordpress site that I've developed and it uses CURL to get share counts for various social networks.
They all appear to work fine apart from LinkedIn - I'm using the following code to get the count:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => 'http://www.linkedin.com/countserv/count/share?url=' . $url . '&format=json'
));
$response = curl_exec($curl);
$results = json_decode($response);
curl_close($curl);
return $results->count;
The curious thing is that this works perfectly on my staging server but on live, I get nothing back. I'd have it down as something server related but then the same code works for Facebook.
On my staging server I had to add the CURLOPT_FOLLOWLOCATION => true
line to my code to get it to work but unfortunately it didn't fix it on the live server.
Any ideas?
First up make sure that the URL is run through urlencode
. It'll take care of issues with special characters like ?
and #
.
You should add error handling code similar to the following after the curl_exec
call, both for debugging and to provide a fallback when curl errors out.
if(curl_errno($curl))
{
// do something sensible here
echo 'error:' . curl_error($c);
}
Assuming you provide the same value for $url
on both staging and production: your code seems fine except for the debug/error handling and url encoding. You should check for an outgoing proxy on your live network or broken/old PHP/libcurl versions.
There has been achange to the linkedIn SSL certificates I believe.
You need to set CURLOPT_SSL_VERIFYPEER
to false
, so in your exmaple that would be:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => 'http://www.linkedin.com/countserv/count/share?url=' . $url . '&format=json'
));
$response = curl_exec($curl);
$results = json_decode($response);
curl_close($curl);
return $results->count;
Normally you would set it as follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);