Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook SDK v4 for PHP Minimal Example

Tags:

namespaces

php

I'm trying to get the minimal example

using Facebook\FacebookSession;

FacebookSession::setDefaultApplication('YOUR_APP_ID','YOUR_APP_SECRET');

// Use one of the helper classes to get a FacebookSession object.
//   FacebookRedirectLoginHelper
//   FacebookCanvasLoginHelper
//   FacebookJavaScriptLoginHelper
// or create a FacebookSession with a valid access token:
$session = new FacebookSession('access-token-here');

// Get the GraphUser object for the current user:

try {
  $me = (new FacebookRequest(
    $session, 'GET', '/me'
  ))->execute()->getGraphObject(GraphUser::className());
  echo $me->getName();
} catch (FacebookRequestException $e) {
  // The Graph API returned an error
} catch (\Exception $e) {
  // Some other error occurred
}

from the README working, but I don't understand what the first line of code means. Where do I have to put the PHP file using that minimal code example within the SDK file structure. I tried directly in the src folder, but that returns the following PHP error

[01-May-2014 20:12:26 Europe/Berlin] PHP Parse error:  syntax error, unexpected 'Facebook' (T_STRING) in /Applications/MAMP/htdocs/facebook-php-sdk-v4/src/test.php on line 9

The file structure looks like this

├── src
│   ├── Facebook
│   │   ├── FacebookAuthorizationException.php
│   │   ├── FacebookCanvasLoginHelper.php
│   │   ├── FacebookClientException.php
│   │   ├── FacebookJavaScriptLoginHelper.php
│   │   ├── FacebookOtherException.php
│   │   ├── FacebookPermissionException.php
│   │   ├── FacebookRedirectLoginHelper.php
│   │   ├── FacebookRequest.php
│   │   ├── FacebookRequestException.php
│   │   ├── FacebookResponse.php
│   │   ├── FacebookSDKException.php
│   │   ├── FacebookServerException.php
│   │   ├── FacebookSession.php
│   │   ├── FacebookThrottleException.php
│   │   ├── GraphLocation.php
│   │   ├── GraphObject.php
│   │   ├── GraphSessionInfo.php
│   │   ├── GraphUser.php
│   │   └── fb_ca_chain_bundle.crt
│   └── test.php
like image 415
Nick Weisser Avatar asked May 01 '14 18:05

Nick Weisser


1 Answers

have recently solved this. as there is autoload.php file available with sdk you dont need to use require etc etc. just include that autoload.php on the start

 <?php
session_start();
// added in v4.0.0
require_once 'autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;

// start session

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

// login helper with redirect_uri

    $helper = new FacebookRedirectLoginHelper('http://yourhost/facebook/' );

try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}

// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();

  // print data
  echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}

?>

after this you must have to check the path in autoload.php file

$base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : __DIR__ . '/src/Facebook/';

this line is default code if u have changed the name of directories like placed all the files from /src/Facebook/ to /sdk/ then just replace the name always check the included path by using die(__DIR__ . '/src/Facebook/'); to make sure if it is correct.

like image 110
zoomi Avatar answered Sep 22 '22 12:09

zoomi