/** * login with token * * @param String $accessToken, $appId, $appSecret * * @return boolean */ public function loginWithToken($accessToken, $appId, $appSecret) { FacebookSession::setDefaultApplication($appId, $appSecret); $session = new FacebookSession($accessToken); $FacebookRequest = new FacebookRequest($session, 'GET', '/me'); $response = $FacebookRequest->execute(); $this->graph = $response->getGraphObject(GraphUser::classname()); if ($session) { return true; } return false; }
FacebookSession::setDefaultApplication($app_id, $app_secret); $helper = new FacebookRedirectLoginHelper($redirect_url); $sess = $helper->getSessionFromRedirect(); //check if facebook session exists if (isset($_SESSION['fb_token'])) { $sess = new FacebookSession($_SESSION['fb_token']); } //logout $logout = 'http://packetcode.com/apps/newfblogin&logout=true'; //4. if fb sess exists echo name if (isset($sess)) { //store the token in the php session $_SESSION['fb_token'] = $sess->getToken(); //create request object,execute and capture response $request = new FacebookRequest($sess, 'GET', '/me'); // from response get graph object $response = $request->execute(); $graph = $response->getGraphObject(GraphUser::classname()); // use graph object methods to get user details $name = $graph->getName(); $id = $graph->getId(); $image = 'https://graph.facebook.com/' . $id . '/picture?width=300'; $email = $graph->getProperty('email'); echo "hi {$name} <br>"; echo "your email is {$email} <br><Br>"; echo "<img src='{$image}' /><br><br>"; echo "<a href='" . $logout . "'><button>Logout</button></a>"; } else { //else echo login echo '<a href="' . $helper->getLoginUrl(array('email')) . '" >Login with facebook</a>'; }