0

I need to make a login through facebook. And after login the basic info of the user like email, fbid, firstname, lastname and sex will be stored in the local database. But i am not getting email from the graph API.

SDK version is 4.0.0 Callback codes are -

session_start();
// added in v4.0.0
require_once 'autoload.php';
require_once("connection.php");
require_once("functions.inc.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' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://dsbangladesh.com/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,gender,email,first_name,last_name' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();
        $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
        $femail = $graphObject->getProperty('email');    // To Get Facebook email ID
        $fbfname = $graphObject->getProperty('name'); // To Get Facebook First name
        $fblname = $graphObject->getProperty('last_name');
        $sex = $graphObject->getProperty('sex');

    echo $fbid;
    echo $femail;
    echo $sex;

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

No error but only prints fbid and 'name' but not email.

Tanjima Tani
  • 580
  • 4
  • 18

0 Answers0