private function fetchId() { if ($this->get('accessToken') === null) { return null; } $me = (new Facebook\FacebookRequest(new Facebook\FacebookSession($this->get('accessToken')), 'GET', '/me'))->execute()->getGraphObject(Facebook\GraphUser::className()); return $me->getId(); }
/** * This function connect to facebook and retrieves the user info * If user does not exist in chamilo, it creates it and logs in * If user already exists, it updates his info */ function facebookConnect() { global $facebook_config; global $helper; try { $helper = new FacebookRedirectLoginHelper($facebook_config['return_url']); $session = $helper->getSessionFromRedirect(); // see if we have a session if (isset($session)) { // graph api request for user data $request = new FacebookRequest($session, 'GET', '/me?fields=id,first_name,last_name,email,locale'); $response = $request->execute(); // get response $graphObject = $response->getGraphObject(Facebook\GraphUser::className()); $username = changeToValidChamiloLogin($graphObject->getProperty('email')); $email = $graphObject->getProperty('email'); $locale = $graphObject->getProperty('locale'); $language = facebookPluginGetLanguage($locale); if (!$language) { $language = 'en_US'; } //Checks if user already exists in chamilo $u = array('firstname' => $graphObject->getProperty('first_name'), 'lastname' => $graphObject->getProperty('last_name'), 'status' => STUDENT, 'email' => $graphObject->getProperty('email'), 'username' => $username, 'language' => $language, 'password' => 'facebook', 'auth_source' => 'facebook', 'extra' => array()); $chamiloUinfo = api_get_user_info_from_email($email); if ($chamiloUinfo === false) { // we have to create the user $chamilo_uid = external_add_user($u); if ($chamilo_uid !== false) { $_user['user_id'] = $chamilo_uid; $_user['uidReset'] = true; $_SESSION['_user'] = $_user; header('Location:' . api_get_path(WEB_PATH)); exit; } else { return false; } } else { // User already exists, update info and login $chamilo_uid = $chamiloUinfo['user_id']; $u['user_id'] = $chamilo_uid; external_update_user($u); $_user['user_id'] = $chamilo_uid; $_user['uidReset'] = true; $_SESSION['_user'] = $_user; header('Location:' . api_get_path(WEB_PATH)); exit; } } } catch (FacebookRequestException $ex) { echo $ex; } catch (Exception $ex) { // When validation fails or other local issues } }
/** * Sync the new data from a users Facebook profile to the member database. * * @param Facebook\GraphUser $result * @param bool $sync Flag to whether we override fields like first name */ public function updateFacebookFields($result, $override = true) { $this->owner->FacebookLink = $result->getProperty('link'); $this->owner->FacebookUID = $result->getProperty('id'); $this->owner->FacebookTimezone = $result->getProperty('timezone'); if ($override) { $email = $result->getProperty('email'); if ($email && !$this->owner->Email || !Email::validEmailAddress($this->owner->Email)) { $this->owner->Email = $email; } $this->owner->FirstName = $result->getProperty('first_name'); $this->owner->Surname = $result->getProperty('last_name'); } $this->owner->extend('onUpdateFacebookFields', $result); }
/** * meを取得 * * @return mixed * @throws Facebook\FacebookRequestException */ public function getMe() { $req = new Facebook\FacebookRequest($this->getSession(), 'GET', '/me'); $me = $req->execute()->getGraphObject(Facebook\GraphUser::className()); return $me; //return $this->facebook->api('/me'); }
/** * Retrieve the business token for this user * * @return string|null * @throws Exception */ public function getBizToken() { $session = $this->getSession(); if (empty($session)) { return null; } try { /** @var Facebook\GraphUser $userProfile */ $userProfile = (new Facebook\FacebookRequest($session, 'GET', '/me?fields=token_for_business'))->execute()->getGraphObject(Facebook\GraphUser::className()); $token = $userProfile->getProperty('token_for_business'); } catch (Facebook\FacebookRequestException $e) { WikiaLogger::instance()->warning('Failed to retrieve business token', ['errorCode' => $e->getCode(), 'errorMessage' => $e->getMessage()]); return null; } return $token; }
if (Phpfox::isUser() && $Url->uri() == '/user/setting/' && substr(Phpfox::getUserBy('email'), -3) == '@fb') { (new Core\Route('/user/setting'))->run(function (\Core\Controller $Controller) { return $Controller->render('setting.html'); }); } /** * Controller for the FB login routine */ (new Core\Route('/fb/login'))->run(function (\Core\Controller $Controller) { $helper = new Facebook\FacebookRedirectLoginHelper($Controller->url->make('/fb/auth')); $loginUrl = $helper->getLoginUrl(); header('Location: ' . $loginUrl); exit; }); /** * Auth routine for FB Connect. This is where we either create the new user or log them in if they are already a user. */ (new Core\Route('/fb/auth'))->run(function (\Core\Controller $Controller) { $helper = new Facebook\FacebookRedirectLoginHelper($Controller->url->make('/fb/auth')); $session = $helper->getSessionFromRedirect(); if ($session) { $request = new Facebook\FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); $user = $response->getGraphObject(Facebook\GraphUser::className()); if ($user instanceof Facebook\GraphUser) { $Service = new \Apps\PHPfox_Facebook\Model\Service(); $Service->create($user); $Controller->url->send('/'); } } });
/** * Grabs logged in user info from faceboook if user is logged in * * @param bool, forces a roundtrip to the facebook server, ie. dont use cached info * * @return array, fb userinfo array if logged in, false otherwise */ public function getFbUserInfo($force_reload = false) { // check for cached versions of this, and return it if so if (!empty($this->fb_userinfo) and !$force_reload) { return $this->fb_userinfo; } // make sure we have a fb session, otherwise we cant return any data if (empty($this->fb_session)) { return false; } // attempt to grab userinfo from fb graph api, using FQL try { $user = $this->apiGet('/me', Facebook\GraphUser::className()); $this->fb_userinfo = $user->asArray(); } catch (Exception $e) { $this->handleFacebookException($e); return false; } // now return the user info if we got any return $this->fb_userinfo; }
/** * Create a user based on Facebook information * * @param string $token Facebook access token * * @return bool|ElggUser */ function socialink_facebook_create_user($token) { if (empty($token)) { return false; } if (!socialink_facebook_available()) { return false; } $session = new Facebook\FacebookSession($token); if (empty($session)) { return false; } $request = new FaceBook\FacebookRequest($session, "GET", "/me"); // set correct proxy settings (if needed) $curl_http_client = socialink_facebook_get_curl_http_client(); $request->setHttpClientHandler($curl_http_client); try { $api_result = $request->execute()->getGraphObject(Facebook\GraphUser::className()); } catch (Exception $e) { } if (empty($api_result)) { return false; } // get user information $name = $api_result->getName(); $email = $api_result->getEmail(); if (get_user_by_email($email)) { register_error(elgg_echo("socialink:networks:create_user:error:email")); return false; } $pwd = generate_random_cleartext_password(); $username = socialink_create_username_from_email($email); try { $user_guid = register_user($username, $pwd, $name, $email); if (empty($user_guid)) { return false; } // show hidden entities $access = access_get_show_hidden_status(); access_show_hidden_entities(true); $user = get_user($user_guid); if (empty($user)) { access_show_hidden_entities($access); return false; } // register user's access tokens elgg_set_plugin_user_setting("facebook_access_token", $token, $user_guid, "socialink"); elgg_set_plugin_user_setting("facebook_user_id", $api_result->getId(), $user_guid, "socialink"); // no need for uservalidationbyemail elgg_unregister_plugin_hook_handler("register", "user", "uservalidationbyemail_disable_new_user"); // sync user data socialink_facebook_sync_profile_metadata($user->getGUID()); // trigger hook for registration $params = array("user" => $user, "password" => $pwd, "friend_guid" => 0, "invitecode" => ""); if (elgg_trigger_plugin_hook("register", "user", $params, true) !== false) { access_show_hidden_entities($access); // return the user return $user; } // restore hidden entities access_show_hidden_entities($access); } catch (Exception $e) { } return false; }
#!/usr/bin/php <?php $config = ['git_urls' => ['https://github.com/facebook/facebook-php-sdk-v4.git' => 'facebook-php-sdk/'], 'autoload_config' => ['facebook-php-sdk/src/Facebook/' => 'Facebook'], 'example' => function () { Facebook\FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET'); // Use one of the helper classes to get a FacebookSession object. // FacebookRedirectLoginHelper, FacebookCanvasLoginHelper, FacebookJavaScriptLoginHelper or create a FacebookSession with a valid access token: $session = new Facebook\FacebookSession('access-token-here'); // Get the GraphUser object for the current user: try { $me = (new Facebook\FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(Facebook\GraphUser::className()); echo $me->getName(); } catch (Facebook\FacebookRequestException $e) { // The Graph API returned an error echo $e->getMessage() . PHP_EOL; } catch (\Exception $e) { // Some other error occurred echo $e->getMessage() . PHP_EOL; } var_dump($session); }]; if ($return_config) { return $config; } require_once __DIR__ . '/_yf_autoloader.php'; new yf_autoloader($config);
public function person($fbId) { $profile = (new Facebook\FacebookRequest(new Facebook\FacebookSession(S()->accessToken()), 'GET', '/me'))->execute()->getGraphObject(Facebook\GraphUser::className()); return ['name' => $profile->getName(), 'link' => $profile->getLink()]; }