PHP/Graph API : Why my facebook session doesn't work's? - php

I'm a new developper on Facebook Graph API. I have follow this tutorial : https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0. I have test once and it works. I have modify and retest and it's work. But, the next day, nothing... My session token for facebook have change and I don't know why and how.
There is my code :
// ALL includes and variables
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( $app_ID, $app_secret );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $url );
// generate login url with scope, each permission as element in array
$loginUrl = $helper->getLoginUrl( array($access_right) );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$graphObject = (new FacebookRequest( $session, 'GET', '/me/accounts'))->execute()->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
$page_token = $graphObject['data'][0]->access_token;
echo '<p>Page token : '.$page_token.'</p>';
$session = new FacebookSession($page_token);
/* make the API call */
$request = new FacebookRequest(
$session,
'POST',
'/'.$pageID.'/feed',
array (
'message' => 'This is a test message',
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
My goal is just to put a message on my fan page, and it works the first time. Why the variable "$_SESSION['fb_token']" is now NULL ?? How I can make it work ?
Thanks for response !
Edit 1 :
I discovered that my session have change the CSRF title : 'fb_token' to 'FBRLH_state'. An idea about this change ?

Here is the content of a file I have that logs in users using the latest Facebook PHP-SDK 4.0.9 (I think).
<?php
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/GraphUser.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\GraphUser;
if (!isset($_SESSION['facebookUserId']) || !isset($_SESSION['facebookSession']) || !isset($_SESSION['facebookUserProfile'])) {
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($appId,$appSecret);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $canvasUrl );
try {
$FBSession = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
if (!isset($FBSession)) {
// STEP ONE - REDIRECT THE USER TO FACEBOOK FOR AUTO LOGIN / APP APPROVAL
header('Location: ' . $helper->getLoginUrl());
exit();
} else {
$user_profile = (new FacebookRequest($FBSession, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
$_SESSION['facebookUserId'] = $user_profile->getID();
$_SESSION['facebookSession'] = $FBSession; // I DON'T THINK THIS ACTAULLY WORKS RIGHT
$_SESSION['facebookUserProfile'] = $user_profile;
}
}
I simply include this file as seen below.
include ('SimpleFacebookLogin.php');
I try to save the session and it doesn't save properly in the session so I think I am missing something as well but I can successfully log in after creating a session. I am just not sure how to access it again afterward. I hope this helps.

Actualy works! =) Session is saved on my pc with this piece of script.
I added session_start() and session_write_close() - maybe you missed those?

Related

how to implementation "graph.facebook" in php file

i want to use http://graph.facebook.com/XXX in my site
how i do a php file that have use this system and show my facebook details when i enter the link?
XXX = userrname
i want that the site show the details of the facebook user that enter the site
i tried everything but nothing, i dont want to make ap like this:
<?php
session_start();
require_once 'autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
$api_key = 'FACEBOOK_APP_ID';
$api_secret = 'FACEBOOK_APP_SECRET';
$redirect_login_url = 'http://www.yoursite.com/somefolder/file.php';
// https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0/
// initialize your app using your key and secret
FacebookSession::setDefaultApplication($api_key, $api_secret);
// create a helper opject which is needed to create a login URL
// the $redirect_login_url is the page a visitor will come to after login
$helper = new FacebookRedirectLoginHelper( $redirect_login_url);
// First check if this is an existing PHP session
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from the existing PHP sesson
$session = new FacebookSession( $_SESSION['fb_token'] );
try {
// validate the access_token to make sure it's still valid
if ( !$session->validate() ) $session = null;
} catch ( Exception $e ) {
// catch any exceptions and set the sesson null
$session = null;
echo 'No session: '.$e->getMessage();
}
} elseif ( empty( $session ) ) {
// the session is empty, we create a new one
try {
// the visitor is redirected from the login, let's pickup the session
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $e ) {
// Facebook has returned an error
echo 'Facebook (session) request error: '.$e->getMessage();
} catch( Exception $e ) {
// Any other error
echo 'Other (session) request error: '.$e->getMessage();
}
}
if ( isset( $session ) ) {
// store the session token into a PHP session
$_SESSION['fb_token'] = $session->getToken();
// and create a new Facebook session using the cururent token
// or from the new token we got after login
$session = new FacebookSession( $session->getToken() );
try {
// with this session I will post a message to my own timeline
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
array(
'link' => 'www.finalwebsites.com/facebook-api-php-tutorial/',
'message' => 'A step by step tutorial on how to use Facebook PHP SDK v4.0'
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
// the POST response object
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
$msgid = $graphObject->getProperty('id');
} catch ( FacebookRequestException $e ) {
// show any error for this facebook request
echo 'Facebook (post) request error: '.$e->getMessage();
}
// now we create a second request to get the posted message in return
if ( isset ( $msgid ) ) {
// we only need to the sec. part of this ID
$parts = explode('_', $msgid);
try {
$request2 = new FacebookRequest(
$session,
'GET',
'/'.$parts[1]
);
$response2 = $request2->execute();
$graphObject2 = $response2->getGraphObject();
// the GET response object
echo '<pre>' . print_r( $graphObject2, 1 ) . '</pre>';
} catch ( FacebookRequestException $e ) {
// show any error for this facebook request
echo 'Facebook (get) request error: '.$e->getMessage();
}
}
} else {
// we need to create a new session, provide a login link
echo 'No session, please login.';
}
// use this for testing only
//unset($_SESSION['fb_token']);
thanks,
mtj

Facebook SDK v4 $session not working

Hi I am using facebook SDKv4 for facebook login on my website
After authentication of app the user is not login into my site neither the details are saving to the database and after authentication i can see the login button
<?php
session_start();
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
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/GraphSessionInfo.php' );
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
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\GraphSessionInfo;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('APPID','SECRET');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'example.com' );
/***************************************************session handinling*********************************************/
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
//$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
// $session = new FacebookSession( $session->getToken() );
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
/*****************************************************session handling*****************************************/
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
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fuid= $graphObject->getProperty('id'); // To Get Facebook ID
$fname=$graphObject->getProperty('first_name'); // To Get Facebook full name
$lname=$graphObject->getProperty('last_name'); // To Get Facebook full name
$femail=$graphObject->getProperty('email');
$gender=$graphObject->getProperty('gender');
CODE TO SAVE DETAILS TO DB
/*************************************************************post on users walll******************************************************/
// Publish to User’s Timeline
$request = ( new FacebookRequest( $session, 'POST', '/me/feed', array(
'message' => 'Only TEXT Goes Here'
) ) )->execute();
// Get response as an array, returns ID of post
$response = $request->getGraphObject()->asArray();
print_r( $response );
// Graph API to publish to timeline with additional parameters
$request = ( new FacebookRequest( $session, 'POST', '/me/feed', array(
'name' => 'Name',
'caption' => 'CAtion',
'link' => 'example.com',
'message' => 'msg'
) ) )->execute();
// Get response as an array, returns ID of post
$response = $request->getGraphObject()->asArray();
/****************************************************************post on users wall************************************************/
redirect($site_url);
//echo print_r( $graphObject, 1 );
} else {
// show login url
$permissions = ['email', 'user_likes','publish_actions'];
echo '<img src="'.$img_cdn.'fbcon.png">';
}
?>
Can any one tell where i am doing wrong ?

Post image on facebook user's timeline

I have an app which uses the Facebook PHP API. This is part of my code ,I am not sure what i'm doing wrong or what i'm missing at the moment. I can't seem to post an image on the user's timeline on facebook. Any help would be much appreciated. Many Thanks.
<?php
echo "<p> Hello World!</p>";
// php 5.3 and up can throw an error if this is not set
date_default_timezone_set("Europe/London");
// much of the example code on the web forgets to include these HttpClients, for some reason
require_once( '../docs/Facebook/HttpClients/FacebookHttpable.php' );
require_once( '../docs/Facebook/HttpClients/FacebookCurl.php' );
require_once( '../docs/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( '../docs/Facebook/FacebookSession.php' );
require_once( '../docs/Facebook/FacebookRedirectLoginHelper.php' );
require_once( '../docs/Facebook/FacebookRequest.php' );
require_once( '../docs/Facebook/FacebookResponse.php' );
require_once( '../docs/Facebook/FacebookSDKException.php' );
require_once( '../docs/Facebook/FacebookRequestException.php' );
require_once( '../docs/Facebook/FacebookAuthorizationException.php' );
require_once( '../docs/Facebook/GraphObject.php' );
// This one is also often left out
require_once( '../docs/Facebook/Entities/AccessToken.php');
// store your $HOSTNAME, $APPID and $SECRET in this file:
require_once( '../docs/my_app_credentials.php' );
session_start();
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
$PAGENAME="page2_upload_photo.php";
$REDIRECT_URL="http://".$HOSTNAME.'/'.$PAGENAME;
$IMAGE_FILENAME='#images/Flower.jpg';
$IMAGE_MESSAGE='hello flower';
$helper = new FacebookRedirectLoginHelper( $REDIRECT_URL );
echo "<p> the redirect url is ".$REDIRECT_URL."</p>";
try {
// echo "<p> about to try to get session: the helper variable: </p>";
// var_dump($helper);
$session = $helper->getSessionFromRedirect();
// echo "<p> the session variable:</p>";
// var_dump($session);
}
catch( FacebookRequestException $ex ) {
// When Facebook returns an error
echo "<p> There was a facebook request exception</p>";
}
catch( \Exception $ex ) {
echo "<p> There was a validation failure</p>";
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
echo "<p> Now try to upload a photo to the album /me</p>";
try {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
//$myCurlFile = new CURLFile('#./'.$IMAGE_FILENAME, 'image/jpg');
$myPhoto = "#images/Flower.jpg";
//var_dump($myCurlFile);
$response = (new FacebookRequest(
$session, 'POST', '/me/photos',
array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
)
))->execute()->getGraphObject();
// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '#/path/to/file.name'
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occurred, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
} else {
// show login url
echo 'Login';
}
?>
You haven't set the upload file.
$response = (new FacebookRequest(
$session, 'POST', '/me/photos',
array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
)
))->execute()->getGraphObject();
You enclosed the variables in quotes which will make it a string, so source is getting the literal string $IMAGE_FILENAME
Here is a simple example
$IMAGE_FILENAME='#images/Flower.jpg';
$IMAGE_MESSAGE='hello flower';
$arr = array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
);
$correct_arr = array(
'source'=>$IMAGE_FILENAME,
'message'=>$IMAGE_MESSAGE
);
print_r($arr);
print_r($correct_arr);
Response
Array
(
[source] => $IMAGE_FILENAME
[message] => $IMAGE_MESSAGE
)
Array
(
[source] => #images/Flower.jpg
[message] => hello flower
)

FB login using Facebook SDK php 4.0.9 and Chrome

I am trying to integrate the new FB php SDK version 4.0.9 - everything seems pretty straight forward except one thing which I cannot manage to figure out.
The Facebook login using the Chrome browser needs two tries - I do not know why. I echoed every single step, even monitored my sessions - nothing stands out.
This are the exact steps to reproduce the bug for the code bellow:
log-out from Facebook, close the chrome browser, access the code page, click login, login to Facebook. After being brought back to the redirect page - no session is created.
login again - it works
this problem becomes more frustrating because if I logout from Facebook after a successful login and try again without exiting the browser, everything works fine.
It does not happen with Firefox!
I am using this version: https://developers.facebook.com/docs/php/gettingstarted/4.0.0
!! 4.0.9. extremely different usage than 4.0.0
<?php
session_start();
$appid = 'appid';
$appsecret = 'appsecret';
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookSignedRequestFromInputHelper.php' ); // added in v4.0.9
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
// these two classes required for canvas and tab apps
require_once( 'Facebook/FacebookCanvasLoginHelper.php' );
require_once( 'Facebook/FacebookPageTabHelper.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSignedRequestFromInputHelper; // added in v4.0.9
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// these two classes required for canvas and tab apps
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookPageTabHelper;
// init app with app id and secret
FacebookSession::setDefaultApplication( $appid,$appsecret );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://mypage/code.php' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
echo 'case 0<br/>';
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
echo 'case 1<br/>';
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
echo 'case 2<br/>';
print_r( $ex );
}
}
echo ' avem session? == '.($session ? '1':'0').' == <br/>';
// see if we have a session
if ( isset( $session ) ) {
echo 'case 3<br/>';
// save the session
$_SESSION['fb_token'] = $session->getToken();
if($_SESSION['fb_token']) echo 'avem $_SESSION <br/>';
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
echo 'case 4<br/>';
// show login url
echo 'Login';
}
echo '<br/><br/>';
print_r($_SESSION);

problems creating session with facebook api

I have updated my facebook api and i'm trying to initiate a session like this,
FacebookSession::setDefaultApplication( 'foo foo','foo foo' );
I get this error on that line,
Fatal error: Class 'facebook\src\Facebook\FacebookSession'not found in /foo/foo/foo/header.php on line 41
My file path is correct...
Any ideas?
Here is my full code,
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// include required files form Facebook SDK
// added in v4.0.5
require_once( 'facebook/src/Facebook/FacebookHttpable.php' );
require_once( 'facebook/src/Facebook/FacebookCurl.php' );
require_once( 'facebook/src/Facebook/FacebookCurlHttpClient.php' );
require_once( 'facebook/src/Facebook/FacebookSession.php' );
require_once( 'facebook/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'facebook/src/Facebook/FacebookRequest.php' );
require_once( 'facebook/src/Facebook/FacebookResponse.php' );
require_once( 'facebook/src/Facebook/FacebookSDKException.php' );
require_once( 'facebook/src/Facebook/FacebookRequestException.php' );
require_once( 'facebook/src/Facebook/FacebookOtherException.php' );
require_once( 'facebook/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'facebook/src/Facebook/GraphObject.php' );
require_once( 'facebook/src/Facebook/GraphSessionInfo.php' );
use facebook\src\Facebook\FacebookHttpable;
use facebook\src\Facebook\FacebookCurl;
use facebook\src\Facebook\FacebookCurlHttpClient;
use facebook\src\Facebook\FacebookSession;
use facebook\src\Facebook\FacebookRedirectLoginHelper;
use facebook\src\Facebook\FacebookRequest;
use facebook\src\Facebook\FacebookResponse;
use facebook\src\Facebook\FacebookSDKException;
use facebook\src\Facebook\FacebookRequestException;
use facebook\src\Facebook\FacebookOtherException;
use facebook\src\Facebook\FacebookAuthorizationException;
use facebook\src\Facebook\GraphObject;
use facebook\src\Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'foo foo','foo foo' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.wuno.com/sandbox/' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
} else {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
echo 'sup dude';
?>

Categories