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

Save object within an array from api response

我正在使用 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 或摘要的值。所有其他人都可以通过以下方式与社交名流一起开箱即用:

        $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;

但是我很难过!

非常感谢所有帮助!

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

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