Facebook session dissappears when refreshing the webpage - php

I'm new to the facebook login SDK for PHP and i'm trying to create a "log in with facebook" function on my website.
I'm using this code which works great:
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/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.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\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookHttpable;
FacebookSession::setDefaultApplication('app-id', 'app-secret');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'app-redirect-link' );
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 'Login';
}
If I press "Login" I get my details and everything I need, but as soon as i'm refreshing the page - the "Login" link appears again, as if the session does not exist anymore.
Any help would be appreciated.

Defining a new session and using it helped.

Related

facebook PHP sdk cannot login

I have been trying to use facebook's PHP sdk to login.
Following is one of my codes.
<?php
session_start();
require_once( 'lib/Facebook/FacebookSession.php' );
require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'lib/Facebook/FacebookRequest.php' );
require_once( 'lib/Facebook/FacebookResponse.php' );
require_once( 'lib/Facebook/FacebookSDKException.php' );
require_once( 'lib/Facebook/FacebookRequestException.php' );
require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
require_once( 'lib/Facebook/GraphObject.php' );
require_once('lib/Facebook/GraphUser.php');
require_once('lib/Facebook/GraphSessionInfo.php');
require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'lib/Facebook/Entities/AccessToken.php' );
require_once( 'lib/Facebook/Entities/SignedRequest.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;
use Facebook\GraphSessionInfo;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
// Initialize application by Application ID and Secret
FacebookSession::setDefaultApplication('app_id','app_secret');
// Login Healper with reditect URI
$helper = new FacebookRedirectLoginHelper( 'http://124.125.244.163/rtCamp/home.php' );
try {
$session = $helper->getSessionFromRedirect();
}
catch( FacebookRequestException $ex ) {
// Exception
}
catch( Exception $ex ) {
// When validation fails or other local issues
}
// Checking Session
if(isset($session))
{
// Request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// Responce
$data = $response->getGraphObject();
// Print data
echo print_r( $data, 1 );
}
else
{
// Login URL if session not found
echo 'Login';
}
?>
Now the problem I am facing is that there is some problem with the line where I can get a session.
i.e $session = $helper->getSessionFromRedirect() after the page redirects from facebook.
Now I even implemented this guy's entire github dir with the same login code here
still, my code stops working after that line and there is not a single error displayed. I tried numerous solutions none of them work. I downloaded the stable Facebook PHP SDK 4.0.0 and I am running a home LAMP server with PHP 5.4. I am stuck.
UPDATE: ERROR comes in Apace log
PHP Fatal error: Class
'Facebook\HttpClients\FacebookStreamHttpClient' not found in
/var/www/rtCamp/lib/Facebook/FacebookRequest.php on line 166, referer:
http://124.125.244.163/rtCamp/home.php
<?php
require_once( 'lib/Facebook/FacebookSession.php' );
require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'lib/Facebook/FacebookRequest.php' );
require_once( 'lib/Facebook/FacebookResponse.php' );
require_once( 'lib/Facebook/FacebookSDKException.php' );
require_once( 'lib/Facebook/FacebookRequestException.php' );
require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
require_once( 'lib/Facebook/GraphObject.php' );
require_once('lib/Facebook/GraphUser.php');
require_once('lib/Facebook/GraphSessionInfo.php');
require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'lib/Facebook/Entities/AccessToken.php' );
require_once( 'lib/Facebook/Entities/SignedRequest.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;
use Facebook\GraphSessionInfo;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
session_start();
$app_id='';
$app_secret='';
$redirect_url='http://124.125.244.163/rtCamp/home.php';
// Initialize application by Application ID and Secret
FacebookSession::setDefaultApplication($app_id,$app_secret);
// Login Healper with reditect URI
$helper = new FacebookRedirectLoginHelper($redirect_url);
$session = $helper->getSessionFromRedirect();
// Checking Session
if(isset($session))
{
// Request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// Responce
$data = $response->getGraphObject(GraphUser::classname());
$name=$graph->getName();
echo "hi $name";
// Print data
echo print_r( $data, 1 );
}
else
{
// Login URL if session not found
echo 'Login';
}
/*
try {
$session = $helper->getSessionFromRedirect();
}
catch( FacebookRequestException $ex ) {
// Exception
}
catch( Exception $ex ) {
// When validation fails or other local issues
}
// Checking Session
if(isset($session))
{
// Request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// Responce
$data = $response->getGraphObject();
// Print data
echo print_r( $data, 1 );
}
else
{
// Login URL if session not found
echo 'Login';
}
*/
?>

Facebook login PHP sdk 4.4

I have been trying to test the Facebook PHP login system but keep getting the error:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /xxxxx/httpd.www/Facebook/FacebookRedirectLoginHelper.php on line 146
I have read through several different examples and tried to implement each after first running into this as well as read through the different questions/answers on here all to no avail.
If anyone has ran into this and found a solution or knows of the solution i would much appreciate the pointer.
Edit current code added.
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxx','xxx' );
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;
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://somesite.com/index.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 'Login';
}
I got the same question with you.you can not use the "include" like in old version. In v4.4,they use it like this:
define('FACEBOOK_SDK_V4_SRC_DIR', '/path/to/facebook-php-sdk-v4/src/Facebook/');
require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';
and then ,just the same as you
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
That is all!
reference:Facebook SDK for PHP

I can't get any value from php Facebook graph api

This is my full code.
// include required files form Facebook SDK
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/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.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\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'KEY','SECRET' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'SITE_NAME.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
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', '***/1458726944414455/likes***' );
$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';
}
In * * section above, if I put some Facebook page names or 'me', the code works fine. But when I insert '/1458726944414455/likes' something like this, I can't get any value so the results are always like this
Array ( )
What makes me mad is, /1458726944414455/likes this works fine in https://developers.facebook.com/tools/explorer, which is Graph API explorer. I think token scope is not that different in Graph API explorer and my own website.
What is wrong? I searched every website which wrote about facebook php sdk or graph api, but they are using almost same code with me.
Is this because that I didn't do anything about Review Submission Form? I just using the default App Status & Review. But I thought that it doesn't matter because I don't do login with Facebook something like that. What is wrong? Please help me.
Thank you for reading.

Get page feeds from facebook page

I am using facebook api for getting feeds from facebook page.
Here is the code I am using:
session_start();
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once ('Facebook/GraphObject.php' );
require_once ('Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/Entities/AccessToken.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/HttpClients/FacebookHttpable.php');
require_once( 'Facebook/HttpClients/FacebookCurl.php');
require_once ( 'Facebook/HttpClients/FacebookCurlHttpClient.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;
FacebookSession::setDefaultApplication('app_id', 'app_secret');
$session = new FacebookSession('access_token');
$session = FacebookSession::newAppSession();
// To validate the session:
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
}
$request = new FacebookRequest(
$session,
'GET',
'/{page-id}/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
I have added app_id, app_secret,page-id and access_token in the code. But I am getting the error
Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message '(#803) Some of the aliases you requested do not exist: {page-id}
But if I open https://graph.facebook.com/page-id?access_token=my_access_token, I get the required data.
I am doing all the stuff on localhost. Can some one help me finding the error?

Getting A Users Data With Facebook API

I am currently logging a user in and then getting /me like this,
$graphObject = (new FacebookRequest( $session, 'GET', '/me/tagged_places' ))->execute()->getGraphObject()->asArray();
Then I am printing the data like this,
print_r( $graphObject, 1 )
If I want to get all the data for all the permissions I am asking for how can I do so then call on it when I need it?
When I change /me to /me/tagged_places etc. it works for that individual permission. How can I get them all?
Also once I have it I would like to organize it. Can I put this all into an array and then call each one as needed? If so how?
Here is all of my code,
// 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\FacebookHttpable;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'foo','foo' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.website.com/' );
// 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 with response
$graphObject = (new FacebookRequest( $session, 'GET', '/me/tagged_places' ))->execute()->getGraphObject()->asArray();
}
?>
Not sure whether I understood you correctly, but if you want to query for more fields and connections use the so-called field expansion:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0#fields
For example:
/me?fields=id,first_name,last_name,tagged_places

Categories