Stuart Compaan
Stuart Compaan

Reputation: 187

Twitter api php foreach loop not working for users tweets

I am using the twitter api php abraham libary and i am trying to loop through the users tweets, but i am having trouble.

I am getting all the users tweets from the home timeline using this.

$content = $connection->get('statuses/home_timeline');

echo '<pre>',print_r($content),'</pre>';

If i print out the array...i get this.

Array ( [0] => stdClass Object (    [created_at] => Tue Feb 10 15:31:07 +0000 2015 [id] => 565171109470171136 [id_str] => 565171109470171136 [text] => We are human beings not human doings – let’s start acting like it & take the time simply to be http://t.co/i4HAnApQxp http://t.co/3qSXA4D1qY [source] => Hootsuite [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] => stdClass Object ( [id] => 8161232 [id_str] => 8161232 [name] => Richard Branson [screen_name] => richardbranson [location] => [profile_location] => [description] => Tie-loathing adventurer, philanthropist & troublemaker, who believes in turning ideas into reality. Otherwise known as Dr Yes at @virgin! [url] => http://t.co/pWM2y98gTu [entities] => stdClass Object (

How would i get the name from the std class object using a forech loop?

I have tryed this but it is not working??

foreach($content['user'] as $tweets) {
echo $tweets['name'] . '<br />';

}

Any help would be good :)

Upvotes: 2

Views: 507

Answers (2)

Stuart Compaan
Stuart Compaan

Reputation: 187

Ok this seemed to work ..thank you for your help

foreach($content as $tweets) {

echo $tweets->user->name . '<br />';

echo $tweets->text . '<br />';

}

Upvotes: 2

James Waring
James Waring

Reputation: 356

what you need to do is this;

foreach($content as $tweets) {
    foreach($tweets['user'] as $user) {
        echo $user['name'] . '<br />';
    }
}

Upvotes: 0

Related Questions