0

我在 Laravel 中有一个用于 Facebook 登录的身份验证系统,当具有所有属性的用户登录时工作正常,但是当用户没有填写其中一个属性时,它会引发错误(例如):

未定义的属性:stdClass::$bio

以下是相关代码:

脸书.php:

public function get_user_info(Token_Access $token)
    {
        $url = 'https://graph.facebook.com/me?'.http_build_query(array(
            'access_token' => $token->access_token,
        ));

        $user = json_decode(file_get_contents($url));

        // Create a response from the request
        return array(
            'uid' => $user->id,
            'nickname' => $user->username,
            'name' => $user->name,
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'gender' => $user->gender,
            'email' => $user->email,
            'birthday' => $user->birthday,
            'location' => $user->location->name,
            'description' => $user->bio, //the line giving trouble for a user
            'friends'  => 'https://graph.facebook.com/me/friends?fields=id&access_token='.$token->access_token,
            'image' => 'https://graph.facebook.com/me/picture?width=200&height=200&access_token='.$token->access_token,
            'urls' => array(
            'Facebook' => $user->link,
            ),
        );
    }

Oauth2Controller.php:

if(is_null($check)) {

                  $fan = new Fan;

                  $fan->fbid = $user['uid'];
                  $fan->email = $user['email'];
                  $fan->first_name = $user['first_name'];
                  $fan->last_name = $user['last_name'];
                  $fan->gender = $user['gender'];
                  $fan->birthday = $user['birthday'];
                  $fan->age = $age;
                  $fan->city = $city;
                  $fan->state = $state_abbrev;
                  $fan->image = $user['image'];
                  $fan->friend_ids_url = $user['friends'];
                  $fan->friend_ids_unpacked = $friend_ids;

                  $fan->save();

                  $new = Fan::where('fbid', '=', $user['uid'])->first();

                  Auth::login($new);
                  return Redirect::to('fans/home');

                }

如何检查用户的个人资料中是否包含这些字段,因此不会引发此错误?谢谢你。

4

1 回答 1

2

你能做这个吗?

 'description' => (isset($user->bio) ? $user->bio : NULL)
于 2013-07-18T06:16:25.417 回答