本文整理汇总了PHP中app\models\Profile::createFromFacebook方法的典型用法代码示例。如果您正苦于以下问题:PHP Profile::createFromFacebook方法的具体用法?PHP Profile::createFromFacebook怎么用?PHP Profile::createFromFacebook使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Profile
的用法示例。
在下文中一共展示了Profile::createFromFacebook方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: facebookCallback
public function facebookCallback()
{
try {
$state = $this->security->getToken();
$code = $this->request->getQuery('code');
$error = $this->request->getQuery('error');
$callback = $this->request->getQuery('callback');
if (!empty($error)) {
if ($error === 'access_denied') {
throw new FbCallbackException('FB authorization error: ' . $error);
} else {
throw new FbCallbackException('FB authorization error: ' . $error);
}
}
if (empty($code)) {
throw new FbCallbackException('Ups, something went wrong during authorization');
}
$facebook_response = $this->facebook->getAccessToken($code, $state, $callback);
$access_token = $facebook_response->access_token;
$uid = $this->facebook->getUid($access_token);
$profile = Profile::findFirst(['uid = ?0', 'bind' => [$uid]]);
if (empty($profile)) {
// No user, let's register it
// TODO: encrypt access_token
// TODO: Change default facebook avatar by ours
// TODO: if user doesn't provide us with email we can't register it
// TODO: facebook date format can vary
$me = $this->facebook->me($access_token);
// Create user
$user = new User();
$user = $user->assignFromFacebook($me);
if ($user->save()) {
// Create profile and assign to the user
$profile = new Profile();
$profile->createFromFacebook($me->id, $access_token, $user->id);
return new Response(json_encode($user));
} else {
throw new InvalidFbUser($user);
}
} else {
// User already registered, update access_token
$profile->save(['access_token' => $access_token]);
$user = $profile->getUser();
return new Response(json_encode($user->toArray()));
}
} catch (FbCallbackException $e) {
return $e->returnResponse();
} catch (InvalidFbUser $e) {
return $e->returnResponse();
} catch (\Exception $e) {
return new Response($e->getMessage(), 409);
}
}