PHP api

From Facebook Developer Wiki (FbDevWiki.com)
Jump to: navigation, search

The PHP .api() method can make calls to both the new Graph API and the older REST API. How it distinguishes between them is if the first argument is an array then it's passed to the REST server, otherwise it goes to the Graph API server.

Here's an example of a call to the Graph API:

require './facebook.php';
 
$facebook = new Facebook(array(
  'appId'  => 'YOUR APP ID',
  'secret' => 'YOUR API SECRET',
  'cookie' => true, // enable optional cookie support
));
 
$session = $facebook->getSession();
if ($session) {  // you may also want to check that: $session->['expires'] > time()
  try {
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

Here's a second example of the Graph API with arguments being passed in:

require './facebook.php';
 
$facebook = new Facebook(array(
  'appId'  => 'YOUR APP ID',
  'secret' => 'YOUR API SECRET',
  'cookie' => true, // enable optional cookie support
));
 
$session = $facebook->getSession();
if ($session) {
  $attachment = array(
    'message' => 'this is my message',
    'name' => 'This is my demo Facebook application!',
    'caption' => 'Caption of the Post',
    'link' => 'http://www.lycos.com',
    'description' => 'Test de post depuis application PHP',
    'picture' => 'http://www.lalibre.be/img/logoLaLibre.gif',
    'actions' => array(array('name' => 'Get Search', 'link' => 'http://www.google.com'))
  );
  $result = $facebook->api('/USER_WALL/feed/','post',$attachment);
}

Here's an example of a call to the REST API server.

require './facebook.php';
 
$facebook = new Facebook(array(
  'appId'  => 'YOUR APP ID',
  'secret' => 'YOUR API SECRET',
  'cookie' => true, // enable optional cookie support
));
 
$session = $facebook->getSession();
if ($session) {  // you may also want to check that: $session->['expires'] > time()
  try {
     $some_friends = $facebook->api(array(
          'method' => 'fql.query',
          'query' => 'SELECT uid2 FROM friend WHERE uid1='.$facebook->getUser().' LIMIT 10'
        ));
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}
Personal tools
Namespaces
Variants
Actions
Navigation
Graph API
FQL
Toolbox