PHP stdClass - Yahoo OAuth - php

I am trying to fetch the contact details using Yahoo OAuth in my code. The OAuth is working correctly and I am getting the following stdClass from Yahoo:
stdClass Object (
[profile] => stdClass Object (
[guid] => AA5JFPBIASDFFESAETKHA
[birthYear] => 1980
[birthdate] => 1/31
[created] => 2008-10-08T13:59:36Z
[displayAge] => 32
[gender] => M
[image] => stdClass Object (
[height] => 192
[imageUrl] => http://socialprofiles.zenfs.com/images/636fb037c2791a06fda15adda78b38f0_192.jpg
[size] => 192x192 [width] => 192
)
[location] => Noida, India
[memberSince] => 2002-03-04T11:38:32Z
[nickname] => Himanshu
[profileUrl] => http://profile.yahoo.com/AA5JFPBIZUNNQRHEA6R73NTKHA
[status] => stdClass Object (
[lastStatusModified] => 2011-10-27T17:03:35Z
[linkTo] =>
[message] => Using Yahoo! mail after a long time.... Full of features that Gmail cannot come anywhere close to..... )
[isConnected] => false
)
This is the print_r of the variable $profile on line 164 of my script. Now when I am trying to use this variable in my program, it is giving me an error like this:
Notice: Undefined property: stdClass::$profileUrl in /var/www/vhosts/xyz.com/httpdocs/PHP/yahoo-api-php-client/examples/simpleauth/simpleauth.php on line 165
Line 165 in my php script is $prfurl = $profile->profileUrl;
Can anyone help me what am I doing wrong here? This is Yahoo's standard code and I have not done anything in it.

It should be $prfurl = $profile->profile->profileUrl;

Related

how to get value from this php array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
i have this array from json_decode
stdClass Object (
[coord] => stdClass Object (
[lon] => 8.97
[lat] => 51
)
[weather] => Array (
[0] => stdClass Object (
[id] => 804
[main] => Clouds
[description] => overcast clouds
[icon] => 04d
)
)
[base] => stations
[main] => stdClass Object (
[temp] => 281.17
[pressure] => 1014
[humidity] => 87
[temp_min] => 280.93
[temp_max] => 281.48
)
[visibility] => 7000
[wind] => stdClass Object (
[speed] => 3.6
[deg] => 180
)
[clouds] => stdClass Object (
[all] => 90
)
[dt] => 1445499854
[sys] => stdClass Object (
[type] => 1
[id] => 4954
[message] => 0.0056
[country] => DE
[sunrise] => 1445493535
[sunset] => 1445530644
)
[id] => 2906244
[name] => Herbelhausen
[cod] => 200
)
i have get from it the description
$response_a2->weather[0]->description
but wen am trying to get the temp or pressure or humidity or speed i filed
$response_a2->base->temp
You are looking for
$response_a2->main->temp
Thanks to RiggsFolly for beautifying the array.
next time, write your code like this:
echo "<pre">;
print_r($response_a2);
echo "</pre>";
and your array will look beautiful in the browser
That data does not exist in $response_a2->base->temp
Instead us
$response_a2->main->temp
$response_a2->main->presure
$response_a2->main->humidity
etc
What you are showing us is an object, not an array. Objects work differently from arrays. They use the -> operator to reach certain values.
For example, imagine I've got object $obj and I wanna reach value ['some_value'].
echo $obj->some_value;

fetch data from an object

I am trying to fetch data from user's twitter account with this code
$user_info = $twitteroauth->get('account/verify_credentials');
i get a very long value in an object
stdClass Object
(
[id] => 2999359418
[id_str] => 2999359418
[name] => ABC
[screen_name] => ABC9
[location] =>
[profile_location] =>
[description] => XYZ
[url] =>
[entities] => stdClass Object
(
[description] => stdClass Object
(
[urls] => Array
(
)
)
)
[protected] =>
[followers_count] => 0
[friends_count] => 0
[listed_count] => 0
[created_at] => Wed Jan 28 12:17:43 +0000 2015
)
i wish to retrieve data from it.
eg. i tried to retrieve description by this code but did it wrong, coz i did not get the result. can anyone please tell how to fetch the data
The information is not an array, but an object. You can access the data by the following:
$info = $user_info->description;
You can repeat this by to go further down the chain such as:
$user_info->entities->description, etc.
You are trying to access a object with the array index syntax it will not work this way.
When you are dealing with objects you need to access the array by using "->"
$user_info->name;
$user_info->entities->description;
$user_info->entities->description->urls;
This is the pattern to access to the depth

Unable to Access Key-Value Pair Twitter API User Timeline

I am getting the following undefined index error when I try to access the screen_name key value pair from Twitter API 1.1 User Timeline https://api.twitter.com/1.1/statuses/user_timeline.json
A PHP Error was encountered
Severity: Notice
Message: Undefined index: screen_name
Filename: controllers/search.php
Line Number: 98
I can see the screen_name key when I print_r(); the associative array that stores the results:
Array ( [0] => Array ( [created_at] => Tue Mar 19 21:55:31 +0000 2013
[id] => 3.141330578148E+17 [id_str] => 314133057814798336 [text] =>
Win a 4-Pack of tickets to the #VanAutoShow March 26-31 via #604Now –
http://t.co/2G0pyNNgLl RT to Enter! [source] => Twitter for Mac
[truncated] => [in_reply_to_status_id] => [in_reply_to_status_id_str]
=> [in_reply_to_user_id] => [in_reply_to_user_id_str] => [in_reply_to_screen_name] => [user] => Array ( [id] => 128700677
[id_str] => 128700677 [name] => Bernard Poon [screen_name] =>
StackOverflow [location] => Vancouver, BC [description] => Code Monkey
that calls Vancouver home. Loves Apple products and newbie
photographer. [url] => http://t.co/wlxhr5tpes [entities] => Array (
[url] => Array ( [urls] => Array ( [0] => Array ( [url] =>
http://t.co/wlxhr5tpes [expanded_url] => http://ceciliaandbernard.com
[display_url] => ceciliaandbernard.com [indices] => Array ( [0] => 0
1 => 22 ) ) ) ) [description] => Array ( [urls] => Array ( ) ) )
[protected] => [followers_count] => 157 [friends_count] => 13
[listed_count] => 4 [created_at] => Thu Apr 01 23:16:57 +0000 2010
[favourites_count] => 0 [utc_offset] =>
I am able to access the screen_name key value pair if I use the Users Show TWitter API 1.1 resource with no errors. However, when I use the User Timeline Twitter API 1.1 resource, I get the above undefined index error. Below is a code snippet of the function I'm using to access both of these resources with different results:
function does_user_exist($r)
{
// convert to associative array
// inorder to search for keys
$a = json_decode($r, true);
echo "Screen Name: " . $a['screen_name'];
}
Any assistance will be greatly appreciated. Thanks in advance.
its inside another array. try the following code
echo "Screen Name: " . $a[0]['screen_name'];

How to find out Yahoo email address using Yahoo API PHP

so heres my predicament.
Currently using Yahoo API with PHP in order to use a 'Login with Yahoo' Function on my site.
I have managed to retrieve my entire profile. Trouble is, I can't directly display the primary email associated with the account.
If I use print_r($profile) I get everything dumped from my profile. It looks like this (personal data removed):
stdClass Object ( [uri] => http://social.yahooapis.com/v1/user/*********/profile [guid] => ***** [birthdate] => / [created] => 2010-04-22T20:47:48Z [emails] => Array ( [0] => stdClass Object ( [handle] => **#gmail.com [id] => 1 [type] => HOME ) [1] => stdClass Object ( [handle] => ****#yahoo.com [id] => 2 [primary] => 1 [type] => HOME ) ) [familyName] => Surname [gender] => M [givenName] => Ricki [image] => stdClass Object ( [height] => 192 [imageUrl] => http://l.yimg.com/dh/ap/social/profile/profile_b192.png [size] => 192x192 [width] => 192 ) [lang] => en-US [memberSince] => 2010-04-08T11:22:56Z [nickname] => Ricki [profileUrl] => http://profile.yahoo.com/*********** [updated] => 2013-03-03T18:12:22Z [isConnected] => )
Using $profile->emails->handle yields nothing however. Any ideas on what to do? I can use $profile->emails which returns an Array, displayed simply as 'Array' but if i try to select from the array i get an error:
"Catchable fatal error: Object of class stdClass could not be converted to string"
Anyone?
$profile->emails[0]->handle will give you the first handle in the system.
You can also do foreach($profile->emails as $email) { print $email->handle; }

Access array elements from API

For the life of me I can not figure out how to access the values of this array. Every example the stdClass Object has some type of value. If I try for example $obj->0->0->city; I get an error.
Can someone show me a example how to access [city] => toronto or even [date_created] => 2011-05-03 14:33:58?
I also tried this with no luck.
$object = $buy[1];
$title = $object->title[0];
echo "$title";
Thanks
This is what the api gives me.
stdClass Object
(
[id] => 1
[name] => toronto
[date_modified] => 2011-03-08 13:07:10
[tax_rate_provincial] =>
)
<br/>
Array
(
[0] => stdClass Object
(
[0] => stdClass Object
(
[id] => 28131844
[full_date] => 20110506
[end_date] => 20110511
[city] => toronto
[saved] => 1651
[discount_percentage] => 52
[deal_option] => Array
(
[0] => stdClass Object
(
[id] => 2600
[title] =>
[date_modified] => 0000-00-00 00:00:00
[date_created] => 2011-05-03 14:33:58
[value] => 3150
[price] => 1499
[deal_id] => 28131844
[is_default] => 0
)
)
[options] =>
[option_quantity] =>
[option_remaining] =>
[purchase_limit] => 1
[gift_limit] => 0
There is a special evil syntax to bypass numeric object attributes:
print $obj->{'0'}->{'0'}->city;
Is the correct syntax, and equivalent to the path you already determined.
Your second example is an array however, so it's probably:
print $array[0]->{'0'}->city;
The alternative is always to just foreach over a specific level - that works for objects and arrays likewise.

Categories