从api响应中保存数组中的对象

时间:2018-05-16 16:01:20

标签: php laravel linkedin-api

我在laravel中使用社交名流打电话给LinkedIn的API并获得以下回复:

Laravel\Socialite\Two\User Object
(
[token] => 
[refreshToken] => 
[expiresIn] => 
[id] => 
[nickname] => 
[name] => 
[email] => l
[avatar] => 
[user] => Array
    (
        [emailAddress] => 
        [firstName] =>
        [formattedName] =>
        [headline] =>
        [id] =>
        [industry] =>
        [lastName] =>
        [location] => Array
            (
                [country] =>
                    (
                        [code] =>
                    )
                [name] =>
            )
        [pictureUrl] =>
        [positions] => Array
            (
                [_total] => 1
                [values] => Array
                    (
                        [0] => Array
                            (
                                [company] => Array
                                    (
                                        [name] =>
                                    )
                                [id] =>
                                [isCurrent] =>
                                [location] => Array
                                    (
                                        [country] => Array
                                            (
                                                [code] =>
                                                [name] =>
                                            )
                                        [name] =>
                                    )
                                [startDate] => Array
                                    (
                                        [month] =>
                                        [year] =>
                                    )
                                [summary] => 
                                [title] => 
                            )
                    )
            )

        [publicProfileUrl] =>
        [summary] =>
[avatar_original] => 
)

然后我将响应保存到我的数据库,但无法确定如何正确访问位置,publicProfileUrl或summary的值。所有其他人都与社交名媛一起开箱即用:

        $user = User::where('provider_id', $linkedin_user->id)->first();

    if (!$user){
      $user = new User;
      $user->name = $linkedin_user->getName();
      $user->email = $linkedin_user->getEmail();
      $user->picture = $linkedin_user->getAvatar();
      $user->provider_id = $linkedin_user->getId();
      $user->access_token = $linkedin_user->token;
      $user->save();
    }

我已尝试添加此内容:

$publicProfileUrl = array_get($linkedin_user, 'user.publicProfileUrl', null);

之后:

$user = User::where('provider_id', $linkedin_user->id)->first();

在保存用户之前添加此内容:

$user->linkedin = $linkedin_user[user]->publicProfileUrl;

但我很难过!

非常感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

AbstractUserTwo\User的父级,有一个getRaw方法(https://github.com/laravel/socialite/blob/3.0/src/AbstractUser.php#L106),它将返回简单数组:

$user = $linkedin_user->getRaw();
echo $user['publicProfileUrl'];
print_r($user['positions']);