PHP foreach循环访问Twitter粉丝无法正常工作

时间:2011-02-09 11:19:53

标签: php twitter

正如问题所述,我正在尝试获取每个粉丝的头像,我正在从Twitter发回json,一旦我解码了它,我会尝试遍历所有用户以获取他们的个人资料图片网址,但现在我得到的是没有网址的图片代码,如<img /> "1"<img /> "2"

这是PHP代码:

$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']."' />";

}

以下是我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
        ) 
)

这只是其中一个数组元素,有很多,但这应该足以说明数组结构。

但是,如果我尝试像这样手动访问每个用户,$img = $followers[0]["profile_image_url"];它工作正常,我也检查了我的计数,它工作正常,所以我假设我必须做错了循环?

提前Thanx!

1 个答案:

答案 0 :(得分:7)

我不确定因为我不熟悉twitter api,但我认为你在这里使用foreach错了。 $value不是$followers的数组,而是$followers数组中的项,因此您根本不需要$i变量。你试过了吗?

//$i = -1;

foreach($followers as $value){

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

}