PHP code example of nickdnk / graph-sdk

1. Go to this page and download the library: Download nickdnk/graph-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nickdnk / graph-sdk example snippets




use Facebook\Facebook;
use Facebook\GraphNodes\GraphUser;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;

$fb = new Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v20.0',
  //'default_access_token' => '{access-token}', // optional
]);

// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
//   $helper = $fb->getRedirectLoginHelper();
//   $helper = $fb->getJavaScriptHelper();
//   $helper = $fb->getCanvasHelper();
//   $helper = $fb->getPageTabHelper();

try {

  // If you provided a 'default_access_token', the '{access-token}' is optional.
  $response = $fb->get('/me', '{access-token}');
  
  // To decode the response to a PHP class, provide the class of the root node in the
  // response. You will have to match this manually based on the endpoint you
  // requested. Please do open a pull request if you want to add more types.
  
  /** @var GraphUser $me */
  $me = $response->getGraphNode(GraphUser::class);
  echo 'Logged in as ' . $me->getName() . PHP_EOL;
  echo 'User email is ' . $me->getEmail() . PHP_EOL;
  
} catch (FacebookResponseException $e) {

  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  
} catch (FacebookSDKException $e) {

  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  
}