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?
use packaname\classname
.use
is used to load dynamic classes using spl_autoload_register
autoload.php
or autoload_fb.php
facebook-php-sdk-v4-4.0-dev/
.FACEBOOK_SDK_V4_SRC_DIR
i.e. path of the facebook libraryNote: 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');
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.
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>';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With