/** * Get Facebook user data * * @static * @return mixed */ public static function user() { X4Core_core::auto_load('fbsdk_library'); $fb = new Facebook\Facebook(array('app_id' => FACEBOOK_APP_ID, 'app_secret' => FACEBOOK_SECRET, 'default_graph_version' => 'v2.2')); // already logged with JS $helper = $fb->getJavaScriptHelper(); try { // get the access token from a Cookie $accessToken = $helper->getAccessToken(); //$_SESSION['fb_access_token'] = (string) $accessToken; // Returns a `Facebook\FacebookResponse` object $response = $fb->get('/me?fields=id,first_name,last_name,email,location', $accessToken); $user = $response->getGraphUser(); } catch (Exception $e) { // user not found //echo 'User not found'; //die; return false; } return $user; }
<?php session_start(); require_once __DIR__ . '/src/Facebook/autoload.php'; $fb = new Facebook\Facebook(['app_id' => '150494718639750', 'app_secret' => 'ce13013f03d23d5c354d399a7e7a2138', 'default_graph_version' => 'v2.5']); $helper = $fb->getJavaScriptHelper(); try { $accessToken = $helper->getAccessToken(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); } if (isset($accessToken)) { $fb->setDefaultAccessToken($accessToken); try { $requestProfile = $fb->get("/me?fields=name,email"); $profile = $requestProfile->getGraphNode()->asArray(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); } $_SESSION['name'] = $profile['name']; header('location: ../'); exit; } else {
private function initApi($accessToken = null) { $facebookApi = new Facebook\Facebook(['app_id' => $this->_appid, 'app_secret' => $this->_appkey, 'default_graph_version' => 'v2.5']); if (!$accessToken) { try { $jsHelper = $facebookApi->getJavaScriptHelper(); $accessToken = $jsHelper->getAccessToken(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error FSLTools::returnError(sprintf(Tools::displayError('Facebook Graph returned an error: %s'), $e->getMessage())); } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues FSLTools::returnError(sprintf(Tools::displayError('Facebook SDK returned an error: %s'), $e->getMessage())); } } $facebookApi->setDefaultAccessToken($accessToken); return $facebookApi; }
/** * @Route("/testFbSDK", name="front_testFbSDK") * @Method("GET") */ public function testFbSDKAction() { $fbParam = $this->container->getParameter('fb'); $fb = new Facebook\Facebook(['app_id' => $fbParam['app_id'], 'app_secret' => $fbParam['app_secret'], 'default_graph_version' => $fbParam['app_version']]); $helper = $fb->getJavaScriptHelper(); try { $accessToken = $helper->getAccessToken(); } catch (Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch (Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } if (isset($accessToken)) { // Logged in } return new Response('ok'); }