0

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

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

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;它只会打印单词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

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

4

1 回答 1

2

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

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

foreach($profile->emails as $k){
   echo 'handle: ' . $k->handle . ' id: ' . $k->id . ' type: ' . $k->type . '<br>';
}
于 2013-06-13T21:14:39.967 回答