mousesports
mousesports

Reputation: 1

Twitter API JSON Help

I've been trying to play with the Twitter API a bit and this is what I have so far:

function get_twitter_user_data($user_id, $limit = 3)
    {           
        $twitter_username = 'twitter';

        $twitter_json = @file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?&include_rts=1&screen_name='.$twitter_username.'&count='.$limit);
        $twitter_data = json_decode($twitter_json);

        if ( ! $twitter_data) {
            return array();
        }

        foreach ($twitter_data[0]->user as $user) {
            $image = $user->profile_image_url;
        }

        // doesn't do anything
        echo $image.'<br/><br/>';

        // works
        echo $twitter_data[0]->user->profile_image_url;
    }

Been trying to figure this out far too long. And yes, I've done research but the solutions that I've found have no worked for me. I think maybe I'm just extremely tired and can't see the issue right now.

Anybody mind explaining why looping through $twitter_data doesn't work but output a direct value with $twitter_data[0]->user->profile_image_url does?

Many thanks.

Upvotes: 0

Views: 410

Answers (1)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99889

You have to do this instead:

foreach ($twitter_data as $status) {
    $user = $status->user;

foreach ($twitter_data[0]->user as $user) iterates over the values of the poster of the first status.

Upvotes: 1

Related Questions