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'];
Related
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.
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.
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
I have made a simple facebook register widget.
On register the App sends the data to send.php
On send.php I have
print_r($response);
And I get something like this:
Array
(
[algorithm] => HMAC-SHA256
[expires] => 1367953200
[issued_at] => 1367946138
[oauth_token] => BAAE0refKufgBAORkK7hUaVpF8MnFygoqHAHrO3nRJMyNjvJx6RjiMjoqbz2YlfqeogcIPGJJaIgD0xtxhBj1WRgQ5F5SidjwM7ZCKOyZBlEuatIqIccbjGj2uMV5hqtKtZA1g7hOEqMeZAEwmnO6SgRgsb9ittKZCDnPfoxYxCxZAZBAhIKX457IG5ZB4yknv9FZB8QUG7Pt0mfBRQUYG12KoTmO7QRH20LP65FyPqTi7mAZDZD
[registration] => Array
(
[name] => derp derp
[email] => ddddd#gmail.com
[location] => Array
(
[name] => Vienna, Austria
[id] => 1.1116511224109E+14
)
[gender] => male
[phone] => sss
)
[registration_metadata] => Array
(
[fields] => [ {'name':'name'}, {'name':'email'}, {'name':'location'}, {'name':'gender'}, {'name':'phone', 'description':'Phone Number', 'type':'text'},]
)
[user] => Array
(
[country] => at
[locale] => en_US
)
[user_id] => 100000506481284
)
So the data gets passed without a problem but how can I access a specific value?
For example text book array stuff doesnt appear to be working like:
print $response[0];
Gives me an error:
Notice: Undefined offset: 0 in C:\xampp\htdocs\ddd\send.php on line 38
How else can I access the data?
For example how could I store country, email, name or whatever in their own variables so I can echo them later? Its there but I cant seem to figure out how to "digest" it properly with php.
There is no index 0 in the array you pasted..
Use it like
$email = $response['registration']['email'];
If it was an object instead of an array you would do
$email = $response->registration->email
This is very basic PHP array handling.
http://php.net/manual/en/language.types.array.php
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.