本文整理汇总了PHP中Facebook\Facebook::get方法的典型用法代码示例。如果您正苦于以下问题:PHP Facebook::get方法的具体用法?PHP Facebook::get怎么用?PHP Facebook::get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Facebook\Facebook
的用法示例。
在下文中一共展示了Facebook::get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getUserPagesFromRedirect
public function getUserPagesFromRedirect()
{
$token = $this->getAccessTokenFromRedirect();
try {
$pages = $this->facebook->get('/me/accounts', $token)->getGraphEdge();
return $pages;
} catch (\Exception $e) {
abort(500);
}
}
示例2: getMe
/**
* Return info about facebook user
* @param $fields
* @return array
* @throws Exception
*/
public function getMe($fields)
{
$client = $this->fb->getOAuth2Client();
$accessTokenObject = $this->helper->getAccessToken();
if ($accessTokenObject == null) {
throw new Exception("User not allowed permissions");
}
if ($fields == "" || !is_array($fields) || count($fields) == 0) {
//array is empty
$fields = array(ID);
//set ID field
}
try {
$accessToken = $client->getLongLivedAccessToken($accessTokenObject->getValue());
$response = $this->fb->get("/me?fields=" . implode(",", $fields), $accessToken);
$this->setSocialLoginCookie(self::SOCIAL_NAME);
return $response->getDecodedBody();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
throw new Exception($e->getMessage());
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
throw new Exception($e->getMessage());
}
}
示例3: authorize
public function authorize()
{
try {
$this->accessToken = $this->helper->getAccessToken();
} catch (FacebookResponseException $e) {
// When Graph returns an error
throw new FacebookAuthenticationException('Graph returned an error: ' . $e->getMessage());
} catch (FacebookSDKException $e) {
// When validation fails or other local issues
throw new FacebookAuthenticationException('Facebook SDK returned an error: ' . $e->getMessage());
}
if (!$this->accessToken) {
throw new FacebookAuthenticationException('Access token not received. ' . $this->helper->getError(), $this->helper->getErrorCode());
}
try {
// Returns a `Facebook\FacebookResponse` object
$response = $this->facebook->get('/me?fields=id,name', $this->accessToken);
} catch (FacebookResponseException $e) {
throw new FacebookAuthenticationException('Graph returned an error: ' . $e->getMessage());
} catch (FacebookSDKException $e) {
throw new FacebookAuthenticationException('Facebook SDK returned an error: ' . $e->getMessage());
}
$fbUser = $response->getGraphUser();
if (!($user = $this->doctrine->getRepository('QuizBundle:User')->findOneBySocialId($fbUser['id']))) {
$user = (new Entity\User())->setAccessToken($this->accessToken)->setSocialType(Entity\User::FACEBOOK)->setSocialId($fbUser['id'])->setName($fbUser['name']);
$manager = $this->doctrine->getManager();
$manager->persist($user);
$manager->flush();
}
$token = new SocialToken($user, $this->accessToken, 'facebook', [$this->adminId == $fbUser['id'] ? 'ROLE_ADMIN' : 'ROLE_USER']);
$this->tokenStorage->setToken($token);
}
示例4: checkGrantExtension
/**
* @see OAuth2\IOAuth2GrantExtension::checkGrantExtension
*/
public function checkGrantExtension(IOAuth2Client $client, array $inputData, array $authHeaders)
{
if (!isset($inputData['facebook_access_token'])) {
return false;
}
$this->facebookSdk->setDefaultAccessToken($inputData['facebook_access_token']);
try {
// Try to get the user with the facebook token from Open Graph
$fbData = $this->facebookSdk->get('/me?fields=email,id,first_name,last_name,name,name_format');
if (!$fbData instanceof \Facebook\FacebookResponse) {
return false;
}
// Check if a user match in database with the facebook id
$user = $this->userManager->findUserBy(['facebookId' => $fbData->getDecodedBody()['id']]);
// If none found, try to match email
if (null === $user && isset($fbData->getDecodedBody()['email'])) {
$user = $this->userManager->findUserBy(['email' => $fbData->getDecodedBody()['email']]);
}
// If no user found, register a new user and grant token
if (null === $user) {
// TODO: Create new user
return false;
} else {
// Else, return the access_token for the user
// Associate user with facebookId
$user->setFacebookId($fbData->getDecodedBody()['id']);
$this->userManager->updateUser($user);
return array('data' => $user);
}
} catch (\FacebookApiExceptionion $e) {
return false;
}
}
示例5: getTokenInfo
/**
* @param string $token
*
* @return UserProfileInterface|null
*/
protected function getTokenInfo($token)
{
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
$response = $this->facebook->get('/me?fields=id,name,email,first_name,last_name', $token);
$user = $response->getGraphUser();
// check if we can get user identifier
if (empty($user->getId())) {
return null;
}
// do not accept tokens generated not for our application even if they are valid,
// to protect against "man in the middle" attack
$tokenMetadata = $this->facebook->getOAuth2Client()->debugToken($token);
// this is not required, but lets be sure because facebook API changes very often
$tokenMetadata->validateAppId($this->facebook->getApp()->getId());
$userProfile = new UserProfile();
$userProfile->setIdentifier($user->getId());
$userProfile->setDisplayName($user->getName());
$userProfile->setFirstName($user->getFirstName());
$userProfile->setLastName($user->getLastName());
$userProfile->setEmail($user->getEmail());
// facebook doesn't allow login with not verified email
if (!empty($user->getEmail())) {
$userProfile->setEmailVerified(true);
}
return $userProfile;
} catch (FacebookSDKException $e) {
return null;
}
}
示例6: indexAction
/**
* Index action
* Checks what needs to be done
* Creating a new user or login
*/
public function indexAction()
{
$facebookHelper = $this->facebook->getRedirectLoginHelper();
try {
$accessToken = $facebookHelper->getAccessToken();
if (isset($accessToken)) {
$facebookUser = $this->facebook->get('/me?fields=id,first_name,middle_name,last_name,email', $accessToken)->getGraphUser();
$customer = $this->_getCustomer();
$websiteId = Mage::app()->getWebsite()->getId();
if ($websiteId) {
$customer->setWebsiteId($websiteId);
}
$customer->loadByEmail($facebookUser->getEmail());
if ($customer->getId()) {
$this->_login($facebookUser, $customer);
} else {
$this->_create($facebookUser, $customer);
}
} else {
$this->_redirect('customer/account/login');
}
} catch (Exception $e) {
$this->_redirect('customer/account/login');
}
}
示例7: readStream
/**
* @inheritdoc
*/
public function readStream()
{
$response = $this->facebook->get('/' . $this->page_id . '/posts?limit=' . $this->limit, $this->accesstoken);
$posts = $response->getDecodedBody()['data'];
$result = [];
foreach ($posts as $post) {
$result[] = new GenericPost(new \DateTime($post['created_time']), strstr($post['message'], "\n", true) ?: $post['message'], 'https://www.facebook.com/' . $post['id'], 'facebook');
}
return $result;
}
示例8: loadUserByUsername
/**
* {@inheritDoc}
*/
public function loadUserByUsername($username)
{
try {
$response = $this->facebook->get('/' . $username);
return $response->getGraphNode(FacebookUser::class);
} catch (FacebookResponseException $e) {
$this->logger->info(sprintf('FacebookResponseException: %s', $e->getMessage()));
} catch (FacebookSDKException $e) {
$this->logger->warning(sprintf('FacebookSDKException: %s', $e->getMessage()));
}
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
示例9: registerWithFacebook
/**
* Create a user account for the authenticated Facebook user.
*
* @return \Models\User
* @throws \Facebook\Exceptions\FacebookSDKException
* @throws \Facebook\Exceptions\FacebookSDKException
* @throws \App\Exceptions\AccountDeactivatedException
*/
public function registerWithFacebook()
{
// Load up the facebook sdk
$fb = new Facebook(['app_id' => env('FACEBOOK_APP_ID'), 'app_secret' => env('FACEBOOK_APP_SECRET'), 'default_graph_version' => env('FACEBOOK_DEFAULT_GRAPH_VERSION')]);
// Retrieve the access token
$jsHelper = $fb->getJavaScriptHelper();
$accessToken = $jsHelper->getAccessToken();
if (!$accessToken) {
throw new FacebookSDKException('The access token is invalid.');
}
// Get the profile info
$profileResponse = $fb->get('/me', $accessToken);
if ($profileResponse->getHttpStatusCode() != 200) {
throw new FacebookSDKException('We could not retrieve your profile info.');
}
$profileInfo = $profileResponse->getGraphUser();
// Check if the user is already registered
$user = User::findBySocialAccountIdAndTypeId($profileInfo['id'], SocialAccountType::FACEBOOK);
if ($user && !$user->active) {
throw new AccountDeactivatedException();
}
// Create a new user account or update the existing one
$user = $user ?: new User();
$user->social_account_type_id = SocialAccountType::FACEBOOK;
$user->social_account_id = $profileInfo['id'];
$user->name = $profileInfo['name'];
$user->email = isset($profileInfo['email']) ? $profileInfo['email'] : '';
$user->location_name = isset($profileInfo['location']) ? $profileInfo['location']->getName() : '';
$user->loggedin_at = date('Y-m-d H:i:s');
$user->active = true;
$user->save();
return $user;
}
示例10: facebook
public function facebook()
{
$fb = new Facebook\Facebook(['app_id' => '144053429274589', 'app_secret' => '4ef6916e238aff3b6726dac08b853135', 'default_graph_version' => 'v2.4', 'default_access_token' => 'CAACDBA17B90BAKI0aOXR1vF5zDtZCOKPbWSXopnvvNpBTHZARXVhUVrZBAXn4CB1ZBgsyk13ZA38uZAWoffwchukfajiIOG7cYrNEEAm0CdlHgwDRWeBuD0OZCfT6PB6U2vsE3O45jTgx0YTc24TXEqyZC1ZBIjc9GxD3aSv6WAyIWsZCpAcbnxYPNCdL389FxaRsZD']);
try {
$response = $fb->get('/me?fields=id,name,email');
} catch (Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$me = $response->getGraphUser();
$name = $me['name'];
$email = $me['email'];
$u_name = preg_replace('/@.*$/', '', $me['email']);
$user = new User();
$user->name = $name;
$user->type = 'general';
$user->register_type = 'facebook';
$user->username = $u_name;
$user->email = $email;
$user->password = bcrypt($u_name);
$user->save();
$lastInsertedId = $user->id;
$profile = new Profile();
$profile = $user->profile()->save($profile);
$credentials = array('email' => $email, 'password' => $u_name);
if (Auth::attempt($credentials)) {
//return Redirect::to('home');
return redirect()->intended('home');
}
//echo '<pre>'; print_r($new_name);
//echo 'Logged in as ' . $me['email'];
}
示例11: retriveProfile
/**
*/
public function retriveProfile()
{
$this->logger->debug('retriveProfile ' . "/me?fields=id,name,email,gender");
$response = $this->facebook->get("/me?fields=id,name,email,gender", $this->getAccessToken());
$this->user = $response->getGraphUser();
$userArray = json_decode($this->user->asJson(), true);
$this->logger->debug(print_r($userArray, true));
// remove location info if set.
if (isset($userArray['location'])) {
unset($userArray['location']);
}
$userArray['ip'] = '' . $this->get_ip_address();
// upsert user
$this->upsertUser($userArray);
return $this->user;
}
示例12: fbUserInfoAction
public function fbUserInfoAction()
{
$fb = new Facebook(['app_id' => '1475718472749501', 'app_secret' => 'a67fee083c27186f52030ff3a72f24f9', 'default_graph_version' => 'v2.4']);
try {
$helper = $fb->getJavaScriptHelper();
$accessToken = $helper->getAccessToken();
$fb->setDefaultAccessToken((string) $accessToken);
$response = $fb->get('/me?locale=en_US&fields=name,email');
$userNode = $response->getGraphUser();
$email = $userNode->getField('email');
$name = $userNode->getField('name');
$arr = explode("@", $email);
$login = $arr[0];
$arr2 = explode(" ", $name);
$firstname = $arr2[0];
$lastname = $arr2[1];
return new JsonResponse(['firstname' => $firstname, 'lastname' => $lastname, 'login' => $login, 'email' => $email]);
} catch (FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
exit;
}
示例13: executeFacebookRequest
/**
* @param string $method
* @param string $endpoint
* @param array $parameters
* @param string $token
*
* @return \Facebook\FacebookResponse
*/
private function executeFacebookRequest($method, $endpoint, array $parameters = [], $token = null)
{
if (is_callable($this->logCallback)) {
//used only for debugging:
call_user_func($this->logCallback, 'Facebook API request', func_get_args());
}
if (!$token) {
$token = $this->appToken;
}
switch ($method) {
case 'GET':
$response = $this->fb->get($endpoint, $token);
break;
case 'POST':
$response = $this->fb->post($endpoint, $parameters, $token);
break;
case 'DELETE':
$response = $this->fb->delete($endpoint, $parameters, $token);
break;
default:
throw new \Exception("Facebook driver exception, please add support for method: " . $method);
break;
}
if (is_callable($this->logCallback)) {
call_user_func($this->logCallback, 'Facebook API response', $response->getDecodedBody());
}
return $response;
}
示例14: _getUsername
/**
* Get email address or name based on access token.
*
* @param AccessToken $accessToken
*
* @return mixed
*/
protected function _getUsername($accessToken)
{
$oAuth2Client = $this->_facebook->getOAuth2Client();
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
$user = $this->_facebook->get('/' . $tokenMetadata->getUserId() . '?fields=name,email', $accessToken)->getGraphUser();
return $user->getField('email') !== null ? $user->getField('email') : $user->getField('name');
}
示例15: requestTimelinePics
/**
* Get all images from the timeline picture album as an array with the fields "ID", "Name" and "Source".
* @param int $limit
* @return ArrayList
*/
public function requestTimelinePics($limit = 100, $countLikes = false)
{
try {
// Make the actual API request
$response = $this->api->get('/' . $this->albumId . '/photos?fields=name,source,created_time&limit=' . $limit);
// Get data of album
$album = json_decode($response->getBody())->data;
// Populate ArrayList
$pA = ArrayList::create();
$photo = array();
foreach ($album as $picture) {
$photo['ID'] = $picture->id;
$photo['Source'] = $picture->source;
$photo['Date'] = date('d-m-Y H:i', strtotime($picture->created_time));
if (isset($picture->name)) {
$photo['Name'] = $this->makeLinks($picture->name);
}
// Count likes
if ($countLikes) {
$photo['Likes'] = count(json_decode($this->api->get($picture->id . '/likes')->getBody())->data);
}
$pA->add($photo);
}
return $pA;
} catch (FacebookSDKException $e) {
user_error($e->getMessage());
}
}