Saifullah Alam
Saifullah Alam

Reputation: 301

Can't figure out how to get email from facebook PHP SDK

I want to capture users' email address when they sign up in my site with facebook. I have the following code:

session_start();
added in v4.0.0
require_once 'autoload.php';
/******function to store users' value******/
require_once 'functions.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;
// init app with app id and secret
FacebookSession::setDefaultApplication( '*****app id****','***Secret key****' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://mykadamtala.com/facebook/fbconfig.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();
        $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
        $firstname = $graphObject->getProperty('first_name');
        $lastname = $graphObject->getProperty('last_name');
        $fbfullname = $graphObject->getProperty('name');
        $gender = $graphObject->getProperty('gender');
        $femail = $graphObject->getProperty('email'); //This of course does not work


    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;  
        $_SESSION['EMAIL'] =  $femail; //Nothing stores here
        $_SESSION['GENDER'] = $gender;             
        $_SESSION['FULLNAME'] = $fbfullname;

        adduser($fbid,$firstname, $lastname, $femail);
    /* ---- header location after session ----*/
  header("Location: index.php");
} else {
  $loginUrl = $helper->getLoginUrl();
 header("Location: ".$loginUrl);
}

Users' id , gender, first name, last name are stored just fine. But the email is not captured. Searching here i got this to plug into the script:

 $getemail = $helper->getLoginUrl(array(
   'scope' => 'email
 ));

But i am confused where to include it and how to finally store the email in a variable so my adduser() function can store the email address in database. I will appreciate the help a lot.

Upvotes: 1

Views: 2224

Answers (4)

Trilokynath
Trilokynath

Reputation: 23

Use this code

session_start();
added in v4.0.0
require_once 'autoload.php';
/******function to store users' value******/
require_once 'functions.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;
// init app with app id and secret
FacebookSession::setDefaultApplication( '*****app id****','***Secret key****' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://mykadamtala.com/facebook/fbconfig.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?  fields=id,name,first_name,last_name,gender,email' );
  $response = $request->execute();

     // get response
        $graphObject = $response->getGraphObject();
        $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
        $firstname = $graphObject->getProperty('first_name');
        $lastname = $graphObject->getProperty('last_name');
        $fbfullname = $graphObject->getProperty('name');
        $gender = $graphObject->getProperty('gender');
        $femail = $graphObject->getProperty('email'); //This of course does not work


    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;  
        $_SESSION['EMAIL'] =  $femail; //Nothing stores here
        $_SESSION['GENDER'] = $gender;             
        $_SESSION['FULLNAME'] = $fbfullname;

        adduser($fbid,$firstname, $lastname, $femail);
    /* ---- header location after session ----*/
  header("Location: index.php");
} else {
  $loginUrl = $helper->getLoginUrl(array(
   'scope' => 'email'
  ));
  header("Location: ".$loginUrl);
}

It belongs in your last else block:

...
else {
  $loginUrl = $helper->getLoginUrl(array(
   'scope' => 'email'
  ));
  header("Location: ".$loginUrl);
}

Upvotes: 1

rahul singh
rahul singh

Reputation: 33

Use this parameter for email id ?locale=en_US&fields=name,email

$request = new FacebookRequest( $session, 'GET', '/me?locale=en_US&fields=name,email' );

Upvotes: 1

Dagmara Janaszek
Dagmara Janaszek

Reputation: 51

$request = new FacebookRequest( $session, 'GET', '/me?  fields=id,name,first_name,last_name,gender,email' );
$response = $request->execute();

$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id');
$fbfullname = $graphObject->getProperty('name');full name
$femail = $graphObject->getProperty('email');
$firstname = $graphObject->getProperty('first_name');
$lastname = $graphObject->getProperty('last_name');
$gender = $graphObject->getProperty('gender');

Upvotes: 2

Tobi
Tobi

Reputation: 31489

It belongs in your last else block:

...
else {
  $loginUrl = $helper->getLoginUrl(array(
   'scope' => 'email'
  ));
  header("Location: ".$loginUrl);
}

Upvotes: 0

Related Questions