I\'m trying to get the minimal example
using Facebook\\FacebookSession;
FacebookSession::setDefaultApplication(\'YOUR_APP_ID\',\'YOUR_APP_SECRET\');
// Use
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.
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');
To avoid using require
or require_once
and to have a better project structure you should follow the following steps:
Create composer.json
file in your web application root with the following contents:
{
"require" : {
"facebook/php-sdk-v4" : "4.0.*"
}
}
Call php composer.phar install
src/Acme/App
src/Acme/App/Run.php
with the following contents:<?php
namespace Acme\App;
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
class Run {
const APP_ID = 'app_id';
const APP_SECRET = 'app_secret';
public function __construct()
{
FacebookSession::setDefaultApplication(self::APP_ID, self::APP_SECRET);
// ...your code goes here...
}
}
index.php
file with the following contents:<?php
$loader = require_once 'vendor/autoload.php';
$loader->add('Acme\App', __DIR__.'/src');
$app = new Run();
This will be a skeleton of your Facebook application (all your further classes should follow PSR-4 standard). Using composer's autoloader you will not need to use require_once
for classes in any registered namespace.
P.S. You may want to remove all your libraries from the web root. It will help to prevent direct access from web to them. Then you can just adjust the pathnames and keep only index.php in your web root. I hope, it's clear.
There is a complete (working) example here: http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/
Code:
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' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('APPID','SECRET');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.metah.ch/' );
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>';
}
just put this on include page:
require DIR . '/path/to/facebook-php-sdk-v4/autoload.php';
Also call the class before use, each time you need:
use Facebook\FacebookSession;
Yeah, the new tutorials aren't very helpful.
I'm running through them, myself, and just to get the examples to work, I did:
function facebookLoader($class) {
require "/path/to/facebook-php-sdk-v4-master/src/" . str_replace("\\", "/", $class) . ".php";
}
spl_autoload_register("facebookLoader");
Facebook\FacebookSession::setDefaultApplication([...]
and prepended Facebook\
anywhere where was a class name being called.
Also, there's a part in the tutorial where it says to use:
FacebookRedirectLoginHelper();
When you, in fact, still have to give the thing input:
FacebookRedirectLoginHelper("http://yourRedirectUri.com/");
Got me through the tutorial - ! Now on to figuring out how to get the user's email/complete profile information.