Question

I'm not understanding something about the profile information I am getting from a Yahoo authentication. Specifically, I want to get the email address from the "emails" property.

The "emails" property is an array. When I try to iterate over the array or parse it as I would any other array, I run into a snag:

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

Here is the code in my oauth callback page, up until the line where the error happens:

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>";
}

The output of the foreach group make sense, until it hits some value that is throwing the error:

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

If I var_dump( $profile ); I get the whole package. I don't know if you want to see it, and I don't know if it's important to this question. It looks like my foreach loop hangs on ["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) }

So, I guess my method of parsing this array isn't working for me.

If I try to echo the email, like so: echo $profile->emails; it just prints the word Array.

If I try to loop through the array, like so:

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

I get the error:

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

So, how do I properly convert all of the variables and objects and properties and whatnot in the Yahoo result to PHP variables?

Was it helpful?

Solution

Don't echo out the array objects. The issue here is that your emails key has an array of objects, when you try to echo them, PHP is unsuccessfully trying to convert those objects to strings.

If you need the email data you need to access them in an appropriate way like this:

foreach($profile->emails as $k){
   echo 'handle: ' . $k->handle . ' id: ' . $k->id . ' type: ' . $k->type . '<br>';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top