Odyss3us
Odyss3us

Reputation: 6635

PHP foreach loop to access twitter followers not working

as the question states, I am trying to get the avatars of each of my followers, I am getting json back from twitter, and once I've decoded it, I try to loop through all of the users to get their profile images url, but right now all I am getting are image tags with no URL's, like so, <img /> "1" or <img /> "2".

Here is the PHP code:

$followers = json_decode(file_get_contents("http://api.twitter.com/1/statuses/followers.json?screen_name=usersScreenName"), true);

$i = -1;

foreach($followers as $value){

         $i++;
         echo "<img src='".$value[$i]['profile_image_url']."' />";

}

Here is what I get when I print_r($followers),

Array
(
    [0] => Array
        (
            [contributors_enabled] => 
            [following] => 
            [verified] => 
            [url] => 
            [is_translator] => 
            [time_zone] => timezone
            [profile_text_color] => 739e9f
            [profile_image_url] => http://a3.twimg.com/profile_images/000000000/normal.jpg
            [description] => description
            [status] => Array
                (
                    [truncated] => 
                    [text] => test
                    [geo] => 
                    [favorited] => 
                    [id_str] => 00000000000000
                    [retweet_count] => 0
                    [coordinates] => 
                    [in_reply_to_screen_name] => 
                    [in_reply_to_status_id] => 
                    [source] => web
                    [in_reply_to_status_id_str] => 
                    [created_at] => Wed Feb 09 10:16:51 +0000 2011
                    [contributors] => 
                    [place] => 
                    [retweeted] => 
                    [in_reply_to_user_id_str] => 
                    [in_reply_to_user_id] => 
                    [id] => 5678910
                )

            [notifications] => 
            [profile_sidebar_fill_color] => 3b615d
            [location] => location
            [id_str] => 000000
            [profile_background_tile] => 
            [screen_name] => screen_name
            [created_at] => Sat Apr 18 20:48:58 +0000 2009
            [profile_link_color] => b4d9ad
            [show_all_inline_media] => 
            [follow_request_sent] => 
            [geo_enabled] => 
            [profile_sidebar_border_color] => aef5fa
            [statuses_count] => 1277
            [friends_count] => 37
            [followers_count] => 38
            [protected] => 
            [lang] => en
            [profile_use_background_image] => 1
            [favourites_count] => 38
            [name] => Name
            [profile_background_color] => 214542
            [id] => 000000
            [listed_count] => 3
            [profile_background_image_url] => http://a2.twimg.com/profile_background_images/00000000/qw5ef15qw1fe515weqf1qw5e1f.jpg
            [utc_offset] => 7200
        ) 
)

That is just one of the array elements, there are quite a few, but that should be sufficient to illustrate the array structure.

But if I try to access each users manually like this, $img = $followers[0]["profile_image_url"]; it works fine, I've also checked my count and it is working fine, so I am assuming that I must be doing something wrong with the loop?

Thanx in advance!

Upvotes: 0

Views: 705

Answers (1)

jswolf19
jswolf19

Reputation: 2303

I'm not sure because I'm not familiar with the twitter api, but I think you're using foreach wrong here. $value is not the array of $followers, but an item in the array of $followers, so you should not need the $i variable at all. Have you tried:

//$i = -1;

foreach($followers as $value){

//         $i++;
//         echo "<img src='".$value[$i]['profile_image_url']."' />";
         echo "<img src='".$value['profile_image_url']."' />";

}

Upvotes: 7

Related Questions