如何从Yahoo oAuth2结果解析配置文件对象


How to parse the profile object from a Yahoo oAuth2 result?

我不理解我从雅虎身份验证中获得的个人资料信息。具体来说,我想从"email "属性中获取电子邮件地址。

"email "属性是一个数组。当我尝试遍历数组或像处理任何其他数组一样解析它时,我遇到了一个问题:

Catchable fatal error: Object of class stdClass could not be converted to string in /home/content/54/7550554/html/talqo/youath2callback.php on line 33

下面是我的第一个回调页面中的代码,直到发生错误的那一行:

require("lib/Yahoo.inc");  
// Your Consumer Key (API Key) goes here. 
define('CONSUMER_KEY', "$consumer_key");  
// Your Consumer Secret goes here.  
define('CONSUMER_SECRET', "$consumer_secret");  
// Your application ID goes here.  
define('APPID', "$appId");  
$session = YahooSession::requireSession(CONSUMER_KEY,CONSUMER_SECRET,APPID);  
// Fetch the logged-in (sessioned) user  
$user = $session->getSessionedUser();  
// Fetch the profile for the current user.  
$profile = $user->getProfile();
foreach($profile as $k => $v ){
    echo "$k = $v <br>";
}

foreach组的输出是有意义的,直到它遇到抛出错误的某个值:

uri = http://social.yahooapis.com/v1/user/IRSAJKSJHGAH3OMPDQSQ7RIWV4/profile
guid = IRSAJKSJHGAH3OMPDQSQ7RIWV4
birthdate = 5/13
created = 2012-09-11T13:47:38Z
emails = Array
familyName = Last NameLewis
gender = M
givenName = Gregory
Catchable fatal error: Object of class stdClass could not be converted to string in /home/content/54/7550554/html/talqo/youath2callback.php on line 33

如果我是var_dump( $profile );,我得到了整个包。我不知道你们是否想看,我也不知道这对这个问题是否重要。看起来我的foreach循环挂在["image"]:

object(stdClass)#9 (17) { ["uri"]=> string(70) "http://social.yahooapis.com/v1/user/IRSAJKSJHGAH3OMPDQSQ7RIWV4/profile" ["guid"]=> string(26) "IRSAJKSJHGAH3OMPDQSQ7RIWV4" ["birthdate"]=> string(4) "5/30" ["created"]=> string(20) "2012-09-11T13:47:38Z" ["emails"]=> array(2) { [0]=> object(stdClass)#11 (3) { ["handle"]=> string(27) "primaremail@mydomain.com" ["id"]=> int(1) ["type"]=> string(4) "HOME" } [1]=> object(stdClass)#12 (4) { ["handle"]=> string(20) "secondemail@yahoo.com" ["id"]=> int(2) ["primary"]=> bool(true) ["type"]=> string(4) "HOME" } } ["familyName"]=> string(14) "Last NameLewis" ["gender"]=> string(1) "M" ["givenName"]=> string(7) "Gregory" ["image"]=> object(stdClass)#13 (4) { ["height"]=> int(192) ["imageUrl"]=> string(55) "http://l.yimg.com/dh/ap/social/profile/profile_b192.png" ["size"]=> string(7) "192x192" ["width"]=> int(192) } ["lang"]=> string(5) "en-US" ["memberSince"]=> string(20) "2012-09-11T13:19:09Z" ["nickname"]=> string(7) "Gregory" ["phones"]=> array(1) { [0]=> object(stdClass)#14 (3) { ["id"]=> int(5) ["number"]=> string(12) "1-1234567890" ["type"]=> string(6) "MOBILE" } } ["profileUrl"]=> string(51) "http://profile.yahoo.com/IRSAJKSJHGAH3OMPDQSQ7RIWV4" ["timeZone"]=> string(19) "America/Los_Angeles" ["updated"]=> string(20) "2013-06-13T14:59:34Z" ["isConnected"]=> bool(false) }

所以,我猜我解析这个数组的方法不适合我。

如果我尝试回显电子邮件,像这样:echo $profile->emails;它只是打印单词Array

如果我尝试遍历数组,像这样:

foreach($profile->emails as $k){
echo "$k <br>";
}

我得到错误:

Catchable fatal error: Object of class stdClass could not be converted to string in /home/content/54/7550554/html/talqo/youath2callback.php on line 34

如何将雅虎结果中的变量,对象,属性等转换为PHP变量呢?

不要回显数组对象。这里的问题是,您的emails键有一个对象数组,当您尝试回显它们时,PHP无法成功地将这些对象转换为字符串。

如果您需要电子邮件数据,您需要以适当的方式访问它们,如:

foreach($profile->emails as $k){
   echo 'handle: ' . $k->handle . ' id: ' . $k->id . ' type: ' . $k->type . '<br>';
}