如何使用Laravel Socialite实现Facebook Api获取生日性别和宗教信息 [英] How to get birthday gender and religion information using laravel socialite implementing facebook Api
问题描述
我刚开始使用laravel,我在使用laravel社交名流.
I just started using laravel, I am using laravel socialite.
我可以使用以下代码获取用户名和其他数据.但是,如何获取不在$ user变量中的其他数据,如生日,性别,宗教信仰等?
I am able to get username and other data by using following code. But how do I get other data like birthday, gender, religion etc that is not in $user variable ??
我的代码
$user = Socialite::with('facebook')->user();
return "<h2> User Name ".$user->getName()."</h2><h2> ID :".$user->getId()."</h2><h2>Full Object: ".var_dump($user)"About".$user->getAbout();
我的输出:
object(Laravel\Socialite\Two\User)#170 (10) { ["token"]=> string(162) "EAADg3CcIylABAESNrt1ZBsHiLzlR7uu4e4SCjNsU8jJK6zAurEZBGW7BexQiMjiRkrIw57LYPuNrFZBiaW8MIWAh0oIOIIyFF916PH8SDCtU68r83OYBsXbJzS5x4AzIQlpxX8W7V6Mtg446CqwocGXZCHXRFv8ZD" ["refreshToken"]=> NULL ["expiresIn"]=> string(7) "5170770" ["id"]=> string(15) "624135571074425" ["nickname"]=> NULL ["name"]=> string(21) "Muhammad Abdullah Ali" ["email"]=> string(27) "abdullahbinali3@hotmail.com" ["avatar"]=> string(67) "https://graph.facebook.com/v2.6/624135571074425/picture?type=normal" ["user"]=> array(5) { ["name"]=> string(21) "Muhammad Abdullah Ali" ["email"]=> string(27) "abdullahbinali3@hotmail.com" ["gender"]=> string(4) "male" ["verified"]=> bool(true) ["id"]=> string(15) "624135571074425" } ["avatar_original"]=> string(66) "https://graph.facebook.com/v2.6/624135571074425/picture?width=1920" }
用户名Muhammad Abdullah Ali
User Name Muhammad Abdullah Ali
ID:624135571074425
ID :624135571074425
推荐答案
在社交名流中,重定向至Facebook方法:
In socialite redirect to facebook method :
public function redirectToFacebook()
{
return Socialite::driver('facebook')->fields([
'first_name', 'last_name', 'email', 'gender', 'birthday'
])->scopes([
'email', 'user_birthday'
])->redirect();
}
获取生日.
然后在您自己的模型中使用它:
Then to use it in your own models :
public function handleFacebookCallback()
{
$facebook_user = Socialite::driver('facebook')->fields([
'first_name', 'last_name', 'email', 'gender', 'birthday'
])->user();
}
注意:不过,当用户连接到Facebook时,它将要求用户接受分享他们的生日.
Note: it will ask for the user to accept sharing their birth date when they connect to Facebook though.
这篇关于如何使用Laravel Socialite实现Facebook Api获取生日性别和宗教信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!