10

I am having a hard time with facebook's SDK documentation. I downloaded the SDK from Github and added it into my PHP project.

Here is the file system:

   ├── 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

here is my code so far:

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

FacebookSession::setDefaultApplication('*******','******');

$helper = new FacebookRedirectLoginHelper('http://isgeek.eu/fb/FaRepost/return.php');
$loginUrl = $helper->getLoginUrl();
// Use the login url on a link or button to redirect to Facebook for authentication

I get this error

Fatal error: Class 'Facebook\FacebookSession' not found in /homepages/2/d184071366/htdocs/isgeek/fb/FaRepost/test.php on line 9

At updated my PHP version, so the issue does not comme from here. It seems like the PHP files are not found. I read this question (Facebook SDK v4 for PHP Minimal Example) but it does not help.

Where does this comme from?

Community
  • 1
  • 1
Paul Fournel
  • 10,807
  • 9
  • 40
  • 68
  • what is there in facebooksession file,is there any class defined with this name? – ɹɐqʞɐ zoɹǝɟ May 09 '14 at 17:01
  • possible duplicate of [Facebook SDK v4 for PHP Minimal Example](http://stackoverflow.com/questions/23413854/facebook-sdk-v4-for-php-minimal-example) – Rajesh Ujade Sep 09 '14 at 05:12
  • 1
    The FacebookSession class is no longer available in v5.x of the SDK (which is still under the php-sdk-v4 name). A blog post that will help you move from 4.x to 5.x of the SDK can be found here. https://www.sammyk.me/upgrading-the-facebook-php-sdk-from-v4-to-v5 – Bil Simser Aug 13 '15 at 02:18

7 Answers7

20
  1. PHP Version 5.4.0 or higher is required.
  2. Facebook uses Implementations of PSR-4. Hence You dont have to use require or require_once or include or include_once.
  3. In PSR-4, you just need packagename(namespace) i.e. directory name and class file name only.It will register classes dynamically from given package name.Ex.:- use packaname\classname.
  4. You will find the file autoload.php in Facebook SDK Autoload root directory.
  5. use is used to load dynamic classes using spl_autoload_register
  6. Facebook register all library using autoload.php or autoload_fb.php
  7. You have to find autoload.php in your downloaded library like facebook-php-sdk-v4-4.0-dev/.
  8. If you just wants to use Facebook library from download source.Then you have to copy autoload.php in your root directory or in Facebook directory.
  9. defined constant for FACEBOOK_SDK_V4_SRC_DIR i.e. path of the facebook library
  10. You need to do as below to use in php

Note: I have copied /var/www/stack/24006673/facebook-php-sdk-v4-4.0-dev/src/Facebook directory and /var/www/stack/24006673/facebook-php-sdk-v4-4.0-dev/autoload.php file in root directory /var/www/stack/24006673/

define('FACEBOOK_SDK_V4_SRC_DIR','/var/www/stack/24006673/Facebook/');
require_once("autoload.php");
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication('YOUR_APP_ID','YOUR_APP_SECRET');
Rajesh Ujade
  • 2,715
  • 19
  • 39
14

I found the solution here

I did not code in php for some time now and things have changed. use Facebook\FacebookSession; is not enough. You need to add a require_once too.

require_once( 'Facebook/FacebookSession.php' );

Edit: for a more detailed solution, please checkout the answer below.

Paul Fournel
  • 10,807
  • 9
  • 40
  • 68
  • 1
    Yeah it's kind of weird that in none of the Facebook documentation they mention which files to include! – wbinky Jun 12 '14 at 15:29
  • 1
    You have to include **autoload.php** from your downloaded library like facebook-php-sdk-v4-4.0-dev/. Answer is provided as below – Rajesh Ujade Sep 08 '14 at 08:31
  • @rajeshujade can you please post your sample code here, i did that and its not working ,i tried almost all the ways, it still says same error class not found – Iftikar Urrhman Khan Oct 05 '14 at 13:33
  • @IftikarUrrhmanKhan I have added the answer to this question. Also you can check this link - http://stackoverflow.com/questions/23413854/facebook-sdk-v4-for-php-minimal-example/25737047#25737047 – Rajesh Ujade Oct 06 '14 at 05:50
3

This code worked for me

session_start();

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.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\FacebookHttpable;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookCurl; 

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('XXXX', 'XXXXXXXXXXXX');

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://localhost/demo/demo2/demo2.php' );

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  print_r( $graphObject, 1 );
} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}
www.amitpatil.me
  • 3,001
  • 5
  • 43
  • 61
  • Do not use `require_once()`. That's not Facebook suggests. Use namespace autoload approach instead. – Raptor Jul 17 '15 at 03:09
3

You should be careful with all the paths!

I cannot see "autoload.php" in your file system. In my case I put the content of PHP SDK in the directory "fb", and use the paths (added DIR . into the "define" line in comparison with the example of https://developers.facebook.com/docs/php/gettingstarted/4.0.0)

define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/fb/src/Facebook/');
require __DIR__ . '/fb/autoload.php';

echo FACEBOOK_SDK_V4_SRC_DIR; //to check if the paths are correct

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
<...>

It helped to avoid that kind of the error.

Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53
0

No need to use require or include. I solved it. Just use following line at the top of your script:

define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/facebook-php-sdk-v4/src/Facebook/');

and you are done.

Arvind Bhardwaj
  • 5,231
  • 5
  • 35
  • 49
0
require_once 'Facebook/autoload.php';
$fb = new Facebook\Facebook([
  'app_id' => '{app_id}',
  'app_secret' => '{app_secret}',
  'default_graph_version' => 'v2.2',
]);

-just add this code .it works. enter your app_id and app_secret of your facebookAPP

Gaurav
  • 1
-1

Rename all Facebook files and directories to lower case. Auto-loading on *nix automatically lowercases all files names.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • No, just no. How can you update the next version then? – Raptor Jul 17 '15 at 03:11
  • You can always repeat the process after updating. I admit this was a quick fix I did without investigating if there is a better solution, but this solution worked perfectly. – rustyx Jul 20 '15 at 13:04