-1

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

4

1 回答 1

0

我用它在 /login 页面上登录我的用户(加上 facebook 在 facebook dev 门户上提供的 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);
    }
}
于 2012-08-07T21:45:17.147 回答