using facebook SDK to call Graph API / FQL Query

Costas

Administrator
Staff member
reference
http://developers.facebook.com/docs/facebook-login/access-tokens
http://developers.facebook.com/docs/php/FacebookSession/4.0.0
http://developers.facebook.com/docs/graph-api/reference/v2.3/post
http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/
http://www.sammyk.me/facebook-sdk-v4-for-php-what-you-need-to-know
http://www.sammyk.me/access-token-handling-best-practices-in-facebook-php-sdk-v4



facebook writes :

Version 2.0 of the Facebook Platform API is the last version where FQL will be available. Versions after 2.0 will not support FQL. Please migrate your applications to use Graph API instead of FQL. Please see our changelog for current version information.



First we create an app, going http://developers.facebook.com

snap646.png


App access tokens are used to make requests to Facebook APIs on behalf of an app rather than a user. This can be used to modify the parameters of your app, create and manage test users, or read your application's insights. App access tokens can also be used to publish content to Facebook on behalf of a person who has granted an open graph publishing permission to your application. source http://developers.facebook.com/docs/facebook-login/access-tokens#apptokens

we go to
http://developers.facebook.com/tools/explorer/

the GET should be
JavaScript:
/oauth/access_token?
     client_id={app-id}
    &client_secret={app-secret}
    &grant_type=client_credentials

so by my app created on start I execute the railway :
snap644.png


this access_token is my #App Access Token#

write it down!

on a php file + using facebook-php-sdk-v4-4.0-dev by http://github.com/facebook/facebook-php-sdk-v4

JavaScript:
//test.php
<?php
	require_once 'facebook/autoload.php';

	use Facebook\FacebookRequest;
	use Facebook\FacebookSession;

	// init app with app id and secret
	FacebookSession::setDefaultApplication('app-id', 'app-secret');

	// If you already have a valid access token:
	$session = new FacebookSession('app-access-token');

	// If you're making app-level requests:
	$session = FacebookSession::newAppSession();

	//your graph query here
	$request = new FacebookRequest(
	  $session,
	  'GET',
	  '/4u4/likes?limit=1000&summary=true'
	);
	//facebook restrict the size of query to 1000 records using pagination

	$response = $request->execute();
	$graphObject = $response->getGraphObject();
	
	echo '
' . print_r($graphObject,1) . '</pre>';

with limit=1000
snap647.png


w/o limit (the default)
snap648.png


 

 

source http://stackoverflow.com/a/23528364
doing the same but not use app_token, the app must be accepted by user level then do anything else (run it by login.php) :

JavaScript:
//index.php
<?php
	session_start();

	require_once 'facebook/autoload.php';

	use Facebook\FacebookRequest;
	use Facebook\FacebookSession;
	use Facebook\FacebookRedirectLoginHelper;
	
	// init app with app id and secret
	FacebookSession::setDefaultApplication('app-id', 'app-secret');

	// login helper with redirect_uri
	$helper = new FacebookRedirectLoginHelper('http://x.com/fbPHPcomplete/index.php');

	$session = $helper->getSessionFromRedirect();

	$request = new FacebookRequest(
	  $session,
	  'GET',
	  '/4u4/limit=1000&summary=true'
	);
	
	$response = $request->execute();
	$graphObject = $response->getGraphObject();
	
	echo '
' . print_r($graphObject,1) . '</pre>';
?>

//login.php
<?php
	session_start();

	require 'facebook/autoload.php';

	use Facebook\FacebookSession;
	use Facebook\FacebookRedirectLoginHelper;

	FacebookSession::setDefaultApplication('app-id', 'app-secret');

	$helper = new FacebookRedirectLoginHelper('http://x.com/fbPHPcomplete/index.php');
	$loginUrl = $helper->getLoginUrl();

	echo '[url='' . $loginUrl . '']Log In[/url]';
?>

 

 

Graph API call with old Facebook SDK http://github.com/facebookarchive/facebook-php-sdk

JavaScript:
//test with old facebook SDK
<?php
	session_start();
	
	// include the facebook sdk
	require_once('resources/facebook-php-sdk-master/src/facebook.php');

	// connect to app
	$config = array();
	$config['appId'] = 'app-id';
	$config['secret'] = 'app-secret';
	$config['fileUpload'] = false; // optional
	
	// instantiate
	$facebook = new Facebook($config);
	$pagefeed = $facebook->api("/4u4/likes?limit=1300&summary=true");

  	echo '
' . print_r($pagefeed,1) . '</pre>';

 

 
You can use summary when querying a feed or multiple posts. Specify this in the fields parameter.

source - http://stackoverflow.com/a/18351526

JavaScript:
https://graph.facebook.com/PAGE_ID/feed?fields=comments.limit(1).summary(true),likes.limit(1).summary(true)

This will return a response like this.

{
  "data": [
    {
      ....
      "summary": {
        "total_count": 56
      }
      ...
    }, 
    {
      ....
      "summary": {
        "total_count": 88
      }
      ...
    }
  ]
}

 

 

FQL example - Get how many shares/likes/comments made for a specific post

snap649.png


https://www.facebook.com/PAGE_ID/posts/POST_ID

JavaScript:
// source - http://stackoverflow.com/a/21229441
SELECT like_info.like_count, comment_info.comment_count, share_count 
  FROM stream 
 WHERE post_id = "632261416828769_808723242515918"
 
Top