在Symfony2中使用facebook连接

时间:2012-08-07 07:58:03

标签: facebook symfony

我在Symfony2中使用facebook connect,我已成功完成此操作,但现在我想从Facebook获取登录用户的用户信息。如何从Facebook获取用户的信息?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我用这个来登录/登录页面上的用户(加上Facebook开发门户网站上的facebook提供的javascript):

public function handle(GetResponseEvent $event)
{
    if (null !== $this->securityContext->getToken() || $event->getRequest()->attributes->get('_route') !== 'login') {
        return;
    }

    $this->facebook = new Facebook(array(
      'appId'  => $this->appId,
      'secret' => $this->appSecret,
    ));

    if ($userId = $this->facebook->getUser()) {
        $token = $this->facebookProvider->generateToken($userId);

        if (false === $token = $this->facebookProvider->authenticate($token, false)) {
            $userInfo = $this->facebook->api('/' + $userId);
            $token = $this->facebookProvider->generateToken($userId);

            /** create new user **/
            $user = new User();
            $user->setFacebookId($userId);
            $user->setUsername($userInfo['name']);
            $user->setLang($userInfo['locale']);
            $user->setEmail($userInfo['email']);

            $this->entityManager->persist($user);
            $this->entityManager->flush();

            $token->setUser($user);
        }

        $this->securityContext->setToken($token);
    }
}