I’m having a bit of an issue trying to get some PHP code to work with the Open Graph API provided by Facebook.
I have some PHP code that takes a url and returns the number of comments on that page that were made using the Facebook Comments plugin.
I know the code works, however, there is a small problem.
When I try my code, it doesn’t show any information. When I check in the API Explorer provided by Facebook, the information I want is there. The only difference is that when using the API explorer an Access Token is being used.
The code I’m running is trying to get the Graph data for pages on the same domain. This is not something that requires the person to authorize our application either, as it makes no sense to ask for authorization just to tell someone how many comments have been made on a page.
So my question is, how do I include an access token in the code below:
function fb_comment_count($url = '') {
$filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url);
$json = json_decode($filecontent);
$count = $json->$url->comments;
if ($count == 0 || !isset($count)) {
$count = 0;
}
echo $count;
}
Right now the only problem is with this https://graph.facebook.com/?ids=’ . $url it is returning empty, but the API Explorer shows the proper information when I use the same URL. Can an Access Token be included in this somewhere, or is there something I’m missing?
I actually ended up using this code, which allow me to get more than just comment counts (it uses the Facebook PHP SDK):
The function takes two variables, the URL of the page you are fetching information on, and the stat you want returned, which can be found on this page: http://developers.facebook.com/docs/reference/fql/link_stat/