Obtain Facebook likes via PHP SDK

In an update to this post here is another way to obtain the number of likes for a Facebook page.

I’ll assume that you’ve already registered a Facebook application, have downloaded the PHP SDK and have included for use in your code.

require(‘facebook.php’);

Setup an array with your Facebook applications credentials

$facebook = new Facebook(array(
‘appId’ => ‘000000000’, // Put appID here
‘secret’ => ‘0000c000c000c000c’, // Put your secret key here
));

Decide which page you wish to obtain the number of likes for

$getRequest = $facebook->api(‘/cocacola’); // Lets look at the Facebook page for the popular soft drink
echo $getRequests[‘likes’];

Obviously you can consume, contrast and display this information in more exciting ways than simply echoing it to the screen. Enjoy.

Get Facebook Likes and Shares via Graph API

Facebook Like and Share count can easily be obtained from the Facebook Graph API. This is an extremely quick way to look up the JSON response from Facebook so that you can further process it for your own requirements.

<?php
$URL =’https://graph.facebook.com/cocacola’;
$JSON = file_get_contents($URL);
$Output = json_decode($JSON);
$Likes = 0;
if($Output->likes){
  $Likes = $Output->likes;
}

echo “Number of likes for CocaCola’s Facebook Page = “.$Likes.”</br>”;

$URL2 =’https://graph.facebook.com/http://www.coca-cola.com’;
$JSON2 = file_get_contents($URL2);
$Output2 = json_decode($JSON2);
$Shares = 0;
if($Output2->shares){
  $Shares = $Output2->shares;
}

echo “Number of shares for coca-cola.com = “.$Shares;
?>

UPDATE 29/01/2012: I’ve written a slightly different method using the PHP SDK in this post here

IE blocking iFrame Cookies

Facebook recently moving their applications inside an iFrame has turned web development on its head once again. After being deprecated in XHTML and barely hanging on in HTML iFrames looked like they had all but become extinct. Not so!

So, although other browsers do not block and intercept your cookie within an iFrame (and hence Facebook application) Internet Explorer will do. The problem lies with the W3C standard called Platform for Privacy Preferences (P3P) and you can read about it here if you wish.

In PHP all you will need to ensure Internet Explorer accepts third party cookies is to send the below header:

header(‘P3P:CP=”IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT”‘);

Note: by using such a P3P statement you are making a legal statement about how your application handles cookies and if you are later found in violation of the statement you make, you may find yourself in criminal or civil hot water.

cURL in PHP to access protected sites

So I was trying to use the FaceBook PHP-SDK and ran into an issue. As the cURL was pointing to an HTTPS source I was getting this error:

Failed: Error Number: 60. Reason: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

The problem was that cURL hasnt been configured to trust the servers HTTPS certificate, as by default cURL is not setup to trust any of the Certificate Authorities (CAs)
Browsers dont have this issue as the browser developers were kind enough to include a list of default CAs, however this doesnt help us out at all…

A quick fix is to simply configure cURL to accept any server certificate. Obviously from a security point of view this isnt great, but if you are not passing sensitive information back and forth you should be ok.

Before calling curl_exec() add the following code:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

This causes cURL to blindly accept an server certificate without doing any verification with the CA that issued it.

The Proper Fix
The proper fix is slightly more involved so I plan to cover it at a later stage. If you cant wait that long research the curlopt_cainfo parameter, and obtaining (and saving) a CA certificate to enable cURL to trust it.