如何在PHP中阅读谷歌返回的数据对象

时间:2014-06-03 02:20:02

标签: php object

我正在使用google登录,google在用户授权后返回以下详细信息。

Google_Service_Oauth2_Userinfoplus Object ( 
    [email] => mona123.janorkar@gmail.com 
    [familyName] => Janorkar 
    [gender] => 
    [givenName] => Mona 
    [hd] => 
    [id] => 11018813453254107047543 
    [name] => Mona Janorkar 
    [verifiedEmail] => 1 
    [data:protected] => Array ( 
    [verified_email] => 1 
    [given_name] => Mona 
    [family_name] => Janorkar ) 
    [processed:protected] => Array ( ) 
)

我应该如何读取上面的所有字段,它也有子阵列。

提前谢谢

此致 拉吉

1 个答案:

答案 0 :(得分:1)

如果您有这样的对象。请遵循以下示例:

// THIS IS A SAMPLE, JUST TO SIMULATE how to access values inside
class Google_Service_Oauth2_Userinfoplus {
    public $email = 'mona123.janorkar@gmail.com';
    public $familyName = 'Janorkar';
    public $gender = null;
    public $givenName = 'Mona';
    public $hd = null;
    public $id = '11018813453254107047543';
    public $name = 'Mona Janorkar';
    public $verifiedEmail = 1;
    protected $data = array(
        'verified_email' => 1,
        'given_name' => 'Mona',
        'family_name' => 'Janorkar',

    );
    protected $processed = array();
}

// for example! this is the returned object
$object = new Google_Service_Oauth2_Userinfoplus();

// you can access the properties of the object thru the "->"
echo $object->email; // mona123.janorkar@gmail.com
  

补充信息:此主题也在此处讨论

     

What is the "->" PHP operator called and how do you say it when reading code out loud?

     

Where do we use the object operator "->" in PHP?