fetch data from an object - php

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

Related

How to select a specific property from array

I am using the Twitter API with this code:
$statuses = $connection->get("favorites/list", ["screen_name" => "Ali" , "count" => 2 ]);
print_r ($statuses);
Which gives this result:
Array ( [0] => stdClass Object ( [created_at] => Sun Jun 28 04:56:55 +0000 2020 [id] => 1277103662918238210 [id_str] => 1277103662918238210 [text] => #mu11igan #LAGallday #ali The Church has always been tied to buildings. The Temple (here and the one in heaven.),sy… [truncated] => 1 [entities] => stdClass Object ( [hashtags] => Array ( ) [symbols] => Array ( ) [user_mentions] => Array ( [0] => stdClass Object
But I would like to display only one specific element;
for example only this one [id_str] => 1277103662918238210
You can just do simply like this.
$retValue = {};
$retValue['id_str'] = $statuses[0][id_str]; // or $statuses[0]->id_str
print_r($retValue); // or echo(json_encode($retValue))
Hope this helps you to understand.

how do I navigate and grab infos from an Array like this

Im trying to navigate and fetch info from this array but im not having much success any tips? This is an decoded JSON from Twitter API
I tryed echo $array->statuses[0]->created_at; and it did not work, am i doing something wrong here?
Array
(
[statuses] => Array
(
[0] => stdClass Object
(
[created_at] => Thu Jul 06 04:05:06 +0000 2017
[id] => 882812623959584768
[id_str] => 882812623959584768
[text] => RT #blackdragonsBR: [R6] GG!! Fechamos o mapa Banco por 5x2 e conquistamos a liderança no #ALIENWARENOIGN #GOBD #Acer #Predator https://t.c…
[truncated] =>
[entities] => stdClass Object
(
[hashtags] => Array
(
[0] => stdClass Object
(
[text] => ALIENWARENOIGN
[indices] => Array
(
[0] => 90
[1] => 105
}
)
}
}
}
}
}
You need to access the array by [] not ->. So change the access of statuses like below
$array[statuses][0]->created_at;
You can use json_encode and json_decode at time then it make the variable to array only then you can get the data easily.
Just like this
$array=json_decode(json_encode($array,true),true);
$array[statuses][0]['created_at'];
It will work for you

looping through object in php gives error

Okay.. I did a lot of research but I kept getting errors so I decided to ask the question directly..
I always converted objects to arrays, but I now want to try using an object directly to save website speed.. I got this object:
stdClass Object
(
[date] => Wed, 8 Feb 2017 15:03:44 +0000
[Date] => Wed, 8 Feb 2017 15:03:44 +0000
[subject] => asdasdasd
[Subject] => asdasdasd
[message_id] =>
[toaddress] => "test#hotmail.com"
[to] => Array
(
[0] => stdClass Object
(
[personal] => test#hotmail.com
[mailbox] => test
[host] => hotmail.com
)
)
[fromaddress] => Jason K
[from] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[reply_toaddress] => Jason K
[reply_to] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[senderaddress] => Jason K
[sender] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[Recent] =>
[Unseen] =>
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 1
[MailDate] => 8-Feb-2017 16:03:25 +0100
[Size] => 7887
[udate] => 1486566205
)
I am trying to get the first date (wed, 8 feb 2017), the from->mailbox and the Size. I managed to get the from->mailbox with this code:
foreach($EmailHeaders->from as $from ){
echo $from->mailbox;
}
But I just cant find out how to obtain the other values aswell.. If i try:
foreach($EmailHeaders as $headers){
echo $headers->date;
}
then it doesnt work... Can anyone explain this to me? Sorry if this is already asked a thousand times before, I just cant figure it out..
check this http://php.net/manual/en/function.get-object-vars.php
var_dump(get_object_vars($EmailHeaders ));
It's simple, you don't have to iterate over the direct properties, just use
$EmailHeaders->date
To get the "date", you just need to use $EmailHeaders->date
You don't need a foreach. It's working for "from" because it contains an array of objects.
You can simply do
$EmailHeaders->date
OR
$EmailHeaders->Date
to get the date from your object.
While iterating through loop, your pointer is already on the date item, so you can't point to date index. It will point to the date index under date item.

Getting sugarcrm array output via REST api sorted with PHP correctly

First, I'm using sugarcrm pro 6.5 and accessing via rest v4, so I have this array that's being returned from printing $results that is working fine:
stdClass Object
(
[result_count] => 2000
[total_count] => 3390
[next_offset] => 2000
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 77da301b-83dd-4fe6-e38f-53ba151fb084
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => 77da301b-83dd-4fe6-e38f-53ba151fb084
)
[name] => stdClass Object
(
[name] => name
[value] => Jim Beam
)
[status] => stdClass Object
(
[name] => status
[value] => Dead
)
[website] => stdClass Object
(
[name] => website
[value] => website.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
[1] => stdClass Object
(
[id] => d0ecc069-d556-98f3-41f2-53ba1468327a
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => d0ecc069-d556-98f3-41f2-53ba1468327a
)
[name] => stdClass Object
(
[name] => name
[value] => John Doe
)
[status] => stdClass Object
(
[name] => status
[value] => New
)
[website] => stdClass Object
(
[name] => website
[value] => web.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
I'm using a query from the api to filter the results for the user I'm targeting:
'query' => "leads.assigned_user_id='user_ID-here'",
'order_by' => "date_entered DESC",
This works fine. So I've ran a foreach () statement to retrieve only one field on a button click, which also works just fine. What I really need to accomplish is before this statement, a foreach() command (or something else?) to filter out and retrieve ONLY the "New" results in the status value, and from that group output an array showing only the website field. Seen in the "desired end result section of this question."
This is the code I'm filtering the field I'm targeting and having a new array created with if that helps bridge the gap:
$results = call('get_entry_list', $params, $url);
$eresult = array();
foreach ($results->entry_list as $index=>$value_list) {
$listed = $value_list->name_value_list->website->value;
$eresult[] = $listed;}
So the desired end result based on this data should be:
Array
(
[1] => web.com
)
I'm unsure what I need to do to filter the "Status" field to only then be ran with the $eresult array I created to achieve this. To be clear, everything is working as it should, and my print from $eresult is outputting exactly as it should by returning all results in the website value area, I just need some help to get it sorted before going to that step by sorting it by the "new" status first without all the extra 'stuff,' then sorting out the array in my desired format with the foreach() statement above. I tried to cut out all the other code, as it's a pretty long project, so this should be all the relevant information for the particular goal I need to accomplish in this segment. Any help is greatly appreciated! Thank you!
I've decided to create a second script for this as a temp solution by adding:
'query' => "(leads.assigned_user_id='user_ID-here') AND (status='New')"
So I guess that works, I was trying to avoid calling another script for just one separate function, but it is working fine.

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

Categories