如何解析Yahoo oAuth2结果中的配置文件对象?

时间:2013-06-13 21:01:56

标签: php oauth-2.0 yahoo-api

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

“emails”属性是一个数组。当我尝试迭代数组或像其他任何数组一样解析它时,我遇到了障碍:

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

以下是我的oauth回调页面中的代码,直到发生错误的行:

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;它只打印数组这个词。

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

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

那么,如何将Yahoo结果中的所有变量,对象和属性以及诸如此类的东西正确转换为PHP变量?

1 个答案:

答案 0 :(得分:2)

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

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

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