如何从twitter API输出此数组的内容?

时间:2012-12-08 07:28:21

标签: php twitter twitter-oauth

好的,所以我打电话给Twitter API以返回我朋友的状态,我做了一个var_dump并返回了一个信息数组..但是,我只想抓住我的朋友的PROFILE IMAGEs ..所以这就是我所做的捕捉它并且它不会返回任何东西

$friends = $connection->get('statuses/friends_timeline'); //method that catches my friends'statuses on Twitter.
var_dump($friends);

下面的循环应该显示我朋友的图像..循环下面是var_dump返回给我的样本..我哪里错了???

if( is_array($friends))
{
    shuffle($friends);
}              

for($i=0; $i <=count($friends);$i++)
    if (is_array($friends))
    {
         foreach ($friends[$i] as $key => $value)
         {                                     
             if(array_key_exists("profile_image_url", $friends[$i]))
             {                                   
                 echo "<img src='". $value."' width='100' height='100'/>";
             }

         } 
    }

array(20) {
  [0]=>
  object(stdClass)#7 (19) {
    ["id_str"]=>
    string(18) "277310503989149697"
    ["user"]=>
    object(stdClass)#8 (37) {
      ["id"]=>
      int(48948037)
      ["default_profile"]=>
      bool(false)
      ["following"]=>
      bool(false)
      ["profile_background_tile"]=>
      bool(true)
      ["id_str"]=>
      string(8) "48948037"
      ["url"]=>
      NULL
      ["profile_sidebar_fill_color"]=>
      string(6) "CCD7DB"
      ["profile_image_url"]=>
      string(66) "http://a0.twimg.com/profile_images/1821098433/photo__1__normal.JPG"
      ["utc_offset"]=>
      int(-28800)
      ["contributors_enabled"]=>
      bool(false)
      ["time_zone"]=>
      string(26) "Pacific Time (US & Canada)"
      ["name"]=>
      string(16) "Semirindi Makala"
      ["listed_count"]=>
      int(1)
      ["profile_background_color"]=>
      string(3) "fff"
      ["screen_name"]=>
      string(7) "MASE_85"
      ["geo_enabled"]=>
      bool(false)
      ["protected"]=>
      bool(false)
      ["follow_request_sent"]=>
      bool(false)
      ["location"]=>
      string(13) "Teh internets"
      ["profile_background_image_url"]=>
      string(69) "http://a0.twimg.com/profile_background_images/422730827/photo__1_.JPG"
      ["friends_count"]=>
      int(15)
      ["verified"]=>
      bool(false)
      ["profile_link_color"]=>
      string(6) "0084B4"
      ["statuses_count"]=>
      int(59)
      ["notifications"]=>
      bool(false)
      ["default_profile_image"]=>
      bool(false)
      ["favourites_count"]=>
      int(0)
      ["lang"]=>
      string(2) "en"
      ["profile_background_image_url_https"]=>
      string(71) "https://si0.twimg.com/profile_background_images/422730827/photo__1_.JPG"
      ["profile_use_background_image"]=>
      bool(true)
      ["profile_image_url_https"]=>
      string(68) "https://si0.twimg.com/profile_images/1821098433/photo__1__normal.JPG"
      ["profile_text_color"]=>
      string(6) "333333"
      ["followers_count"]=>
      int(9)
      ["profile_sidebar_border_color"]=>
      string(6) "A8C7F7"
      ["is_translator"]=>
      bool(false)
      ["description"]=>
      string(0) ""
      ["created_at"]=>
      string(30) "Sat Jun 20 06:50:28 +0000 2009"
    }
    ["in_reply_to_user_id"]=>
    NULL
    ["in_reply_to_status_id"]=>
    NULL
    ["coordinates"]=>
    NULL
    ["geo"]=>
    NULL
    ["created_at"]=>
    string(30) "Sat Dec 08 07:15:50 +0000 2012"

    ["retweet_count"]=>
    int(0)
    ["favorited"]=>
    bool(false)
    ["created_at"]=>
    string(30) "Sat Dec 08 06:40:59 +0000 2012"
    ["truncated"]=>
    bool(false)
    ["in_reply_to_screen_name"]=>
    NULL
    ["retweeted"]=>
    bool(false)
    ["text"]=>
    string(29) "Sat, 08 Dec 12 06:40:59 +0000"
    ["place"]=>
    NULL
    ["source"]=>
    string(65) "<a href="http://www.europe-zone.com" rel="nofollow">Madscore1</a>"
    ["in_reply_to_status_id_str"]=>
    NULL
    ["id_str"]=>
    string(18) "277301732894179328"
    ["in_reply_to_user_id"]=>
    NULL
    ["in_reply_to_status_id"]=>
    NULL
    ["id"]=>
    float(2.7730173289418E+17)
  }
  [7]=>
  object(stdClass)#21 (19) {
    ["in_reply_to_status_id_str"]=>
    NULL
    ["user"]=>
    object(stdClass)#22 (37) {
      ["id"]=>
      int(48948037)
      ["followers_count"]=>
      int(9)
      ["following"]=>
      bool(false)
      ["url"]=>
      NULL
      ["profile_sidebar_border_color"]=>
      string(6) "A8C7F7"
      ["profile_image_url_https"]=>
      string(68) "https://si0.twimg.com/profile_images/1821098433/photo__1__normal.JPG"
      ["utc_offset"]=>
      int(-28800)
      ["default_profile"]=>
      bool(false)
      ["profile_background_tile"]=>
      bool(true)
      ["name"]=>
      string(16) "Semirindi Makala"
      ["id_str"]=>
      string(8) "48948037"
      ["profile_sidebar_fill_color"]=>
      string(6) "CCD7DB"
      ["screen_name"]=>
      string(7) "MASE_85"
      ["protected"]=>
      bool(false)
      ["profile_image_url"]=>
      string(66) "http://a0.twimg.com/profile_images/1821098433/photo__1__normal.JPG"
      ["location"]=>
      string(13) "Teh internets"
      ["contributors_enabled"]=>
      bool(false)
      ["listed_count"]=>
      int(1)
      ["time_zone"]=>
      string(26) "Pacific Time (US & Canada)"
      ["profile_background_color"]=>
      string(3) "fff"
      ["geo_enabled"]=>
      bool(false)
      ["notifications"]=>
      bool(false)
      ["follow_request_sent"]=>
      bool(false)
      ["lang"]=>
      string(2) "en"
      ["profile_background_image_url"]=>
      string(69) "http://a0.twimg.com/profile_background_images/422730827/photo__1_.JPG"
      ["friends_count"]=>
      int(15)
      ["statuses_count"]=>
      int(56)
      ["verified"]=>
      bool(false)
      ["profile_link_color"]=>
      string(6) "0084B4"
      ["default_profile_image"]=>
      bool(false)
      ["favourites_count"]=>
      int(0)
      ["profile_background_image_url_https"]=>
      string(71) "https://si0.twimg.com/profile_background_images/422730827/photo__1_.JPG"
      ["profile_use_background_image"]=>
      bool(true)
      ["is_translator"]=>
      bool(false)
      ["description"]=>
      string(0) ""
      ["profile_text_color"]=>
      string(6) "333333"
      ["created_at"]=>
      string(30) "Sat Jun 20 06:50:28 +0000 2009"
    }
    ["contributors"]=>
    NULL
    ["coordinates"]=>
    NULL
    ["geo"]=>
    NULL
    ["retweet_count"]=>
    int(0)
    ["favorited"]=>
    bool(false)
    ["created_at"]=>
    string(30) "Sat Dec 08 06:28:19 +0000 2012"
    ["in_reply_to_user_id_str"]=>
    NULL
    ["truncated"]=>
    bool(false)
    ["in_reply_to_screen_name"]=>
    NULL
    ["retweeted"]=>
    bool(false)
    ["text"]=>
    string(29) "Sat, 08 Dec 12 06:28:18 +0000"
    ["place"]=>
    NULL
    ["source"]=>
    string(65) "<a href="http://www.europe-zone.com" rel="nofollow">Madscore1</a>"
    ["id_str"]=>
    string(18) "277298543398617088"
    ["in_reply_to_user_id"]=>
    NULL
    ["in_reply_to_status_id"]=>
    NULL
    ["id"]=>
    float(2.7729854339862E+17)
  }
  [8]=>
  object(stdClass)#23 (19) {
    ["user"]=>
    object(stdClass)#24 (37) {
      ["id"]=>
      int(48948037)
      ["default_profile_image"]=>
      bool(false)
      ["followers_count"]=>
      int(9)
      ["following"]=>
      bool(false)
      ["url"]=>
      NULL
      ["favourites_count"]=>
      int(0)
      ["profile_background_image_url_https"]=>
      string(71) "https://si0.twimg.com/profile_background_images/422730827/photo__1_.JPG"
      ["profile_sidebar_border_color"]=>
      string(6) "A8C7F7"
      ["utc_offset"]=>
      int(-28800)
      ["profile_background_tile"]=>
      bool(true)
      ["name"]=>
      string(16) "Semirindi Makala"
      ["id_str"]=>
      string(8) "48948037"
      ["is_translator"]=>
      bool(false)
      ["profile_sidebar_fill_color"]=>
      string(6) "CCD7DB"
      ["screen_name"]=>
      string(7) "MASE_85"
      ["protected"]=>
      bool(false)
      ["profile_image_url"]=>
      string(66) "http://a0.twimg.com/profile_images/1821098433/photo__1__normal.JPG"
      ["location"]=>
      string(13) "Teh internets"
      ["contributors_enabled"]=>
      bool(false)
      ["default_profile"]=>
      bool(false)
      ["time_zone"]=>
      string(26) "Pacific Time (US & Canada)"
      ["profile_image_url_https"]=>
      string(68) "https://si0.twimg.com/profile_images/1821098433/photo__1__normal.JPG"
      ["profile_background_color"]=>
      string(3) "fff"
      ["geo_enabled"]=>
      bool(false)
      ["notifications"]=>
      bool(false)
      ["follow_request_sent"]=>
      bool(false)
      ["listed_count"]=>
      int(1)
      ["lang"]=>
      string(2) "en"
      ["profile_background_image_url"]=>
      string(69) "http://a0.twimg.com/profile_background_images/422730827/photo__1_.JPG"
      ["verified"]=>
      bool(false)
      ["profile_link_color"]=>
      string(6) "0084B4"
      ["profile_use_background_image"]=>
      bool(true)
      ["friends_count"]=>
      int(15)
      ["description"]=>
      string(0) ""
      ["statuses_count"]=>
      int(51)
      ["profile_text_color"]=>
      string(6) "333333"
      ["created_at"]=>
      string(30) "Sat Jun 20 06:50:28 +0000 2009"
    }
    ["contributors"]=>
    NULL
    ["coordinates"]=>
    NULL
    ["geo"]=>
    NULL
    ["retweet_count"]=>
    int(0)
    ["favorited"]=>
    bool(false)
    ["created_at"]=>
    string(30) "Sat Dec 08 06:27:07 +0000 2012"
    ["truncated"]=>
    bool(false)
    ["in_reply_to_screen_name"]=>
    NULL
    ["in_reply_to_status_id_str"]=>
    NULL
    ["in_reply_to_user_id_str"]=>
    NULL
    ["retweeted"]=>
    bool(false)
    ["text"]=>
    string(29) "Sat, 08 Dec 12 06:27:07 +0000"
    ["place"]=>
    NULL
    ["source"]=>
    string(65) "<a href="http://www.europe-zone.com" rel="nofollow">Madscore1</a>"
    ["id_str"]=>
    string(18) "277298243392643072"
    ["in_reply_to_user_id"]=>
    NULL
    ["in_reply_to_status_id"]=>
    NULL
    ["id"]=>
    float(2.7729824339264E+17)
  }
  [9]=>
  object(stdClass)#25 (19) {
    ["user"]=>
    object(stdClass)#26 (37) {
      ["id"]=>
      int(48948037)
      ["followers_count"]=>
      int(9)
      ["profile_image_url_https"]=>
      string(68) "https://si0.twimg.com/profile_images/1821098433/photo__1__normal.JPG"
      ["following"]=>
      bool(false)
      ["url"]=>
      NULL
      ["profile_sidebar_border_color"]=>
      string(6) "A8C7F7"
      ["utc_offset"]=>
      int(-28800)
      ["default_profile"]=>
      bool(false)
      ["profile_background_tile"]=>
      bool(true)
      ["name"]=>
      string(16) "Semirindi Makala"
      ["id_str"]=>
      string(8) "48948037"
      ["profile_sidebar_fill_color"]=>
      string(6) "CCD7DB"
      ["screen_name"]=>
      string(7) "MASE_85"
      ["protected"]=>
      bool(false)
      ["profile_image_url"]=>
      string(66) "http://a0.twimg.com/profile_images/1821098433/photo__1__normal.JPG"
      ["location"]=>
      string(13) "Teh internets"
      ["contributors_enabled"]=>
      bool(false)
      ["listed_count"]=>
      int(1)
      ["time_zone"]=>
      string(26) "Pacific Time (US & Canada)"
      ["is_translator"]=>
      bool(false)
      ["profile_background_color"]=>
      string(3) "fff"
      ["geo_enabled"]=>
      bool(false)
      ["notifications"]=>
      bool(false)
      ["follow_request_sent"]=>
      bool(false)
      ["lang"]=>
      string(2) "en"
      ["profile_background_image_url"]=>
      string(69) "http://a0.twimg.com/profile_background_images/422730827/photo__1_.JPG"
      ["friends_count"]=>
      int(15)
      ["statuses_count"]=>
      int(50)
      ["verified"]=>
      bool(false)
      ["profile_link_color"]=>
      string(6) "0084B4"
      ["default_profile_image"]=>
      bool(false)

1 个答案:

答案 0 :(得分:-1)

// ***********************************-={ 2012-12-08 - WB }=-***********************************
// COLLECT THE FREINDS ARRAY
// *********************************************************************************************
$friends = $connection->get('statuses/friends_timeline'); //method that catches my friends'statuses on Twitter.





// ***********************************-={ 2012-12-08 - WB }=-***********************************
// PRINTS THE ENTIRE ARRAY TO THE SCREEN
// *********************************************************************************************
// var_dump($friends); //STILL WORKS, BUT TRY THE METHOD BELOW

//This Is Just A Cleaner Way Of Dumping The Array
print "<PRE><FONT COLOR=ORANGE>"; print_r($friends); print "</FONT></PRE>";




// ***********************************-={ 2012-12-08 - WB }=-***********************************
// TO WORK WITH INDVIDUAL KEY / VALUE PAIRS FROM ARRAY
// AS YOU MENTIONED YOU WERE ATTEMPTING TO GET YOUR FRIENDS PIC
// *********************************************************************************************
print "<BR>".$friends[5];  
print "<BR>".$friends[6];
print "<BR>".$friends[7];
print "<BR>".$friends[8];// This One Should Be What You Are Looking For
print "<BR>".$friends[9];
print "<BR>".$friends[10];





// ***********************************-={ 2012-12-08 - WB }=-***********************************
// NOT NEEDED UNLESS YOUR LOOKING TO RANDOMIZE LATER ON
// *********************************************************************************************
//shuffle($friends);//THIS WILL PRINT THE ARRAY IN A RANDOM ORDER EACH TIME IT IS CALLED






// ***********************************-={ 2012-12-08 - WB }=-***********************************
// YOUR IF STATEMENT IS GOOD PRACTICE.
// SO IF WE DO NOT HAVE THE POPULATED ARRAY, LETS STOP HERE
// *********************************************************************************************
if( is_array($friends)){
    $counter = 0; //LETS ADD A COUNTER TO USE IN THE PRINT STATEMENT BELOW
    foreach ($friends as &$friend_data) {//WORK WITH EACH KEY. THERE ARE ALOT OF WAYS TO DO THIS LIKE WITH for(), or explode(), OR array_walk ETC...
        print "Key: ".$counter."<BR>".$friend_data;
        $counter++; //Just Adds 1 To The Counter For Each Loop

    }//CLOSE YOUR FOR EACH STATEMENT
}    //CLOSE YOUR IF ARRAY EXISTS STATEMENT










// ***********************************-={ 2012-12-08 - WB }=-***********************************
// FINALY I CLEARLY SEE IN YOUR CODE AS WELL AS YOUR STATEMENT YOU WANT ONE KEY VALUE PAIR.
// SO HERE IS WHAT YOU ASKED FOR IN YOUR POST.
// *********************************************************************************************
$friends = $connection->get('statuses/friends_timeline'); //COLLECT DATA. POPULATE YOUR ARRAY
//var_dump($friends);   //REMOVE COMMENT IF THE KEY/VALUE PAIR IS WRONG AND SELECT THE RIGHT ONE.
$value = $friends[8];   //stick the right key val pair together and load it into the variable $value
print "<img src='". $value."' width='100' height='100'/>"; //PRINT YOUR BUDDIES PIC TO THE PAGE.

它看起来像#8但我可能错过了计数。

请允许我解释。上面的代码将在Var Dump下输出6个键到您的浏览器窗口。数组包含键值对,以便查看您想要的类型$ arrayname [#];

所以id_str会成为朋友[5]。

将此打印到您的浏览器将使其清晰,但就像我说的那样。希望这有助于。

EDIT。 我打字就像这样,这就是我做事的方式。它也属于言论自由法案。如果您无法清楚地阅读此页面,那么本页面上有一个选项可以发布您自己的答案。如果我花时间和努力输入任何东西。我希望它能保持我输入的方式。我会继续按需要修复它。

那些没有比这更好的人却批评别人做事的方式需要生活。我是一名程序员,就像本网站上的大多数其他人一样,他花时间从我的工作日程中抽出时间来帮助其他人解决我已经存在的问题或者今天的问题。

我也有时间跟进这个看起来对我来说很难的家伙。我很快就发现并非如此。他对每个人都只是个傻瓜。

所以,如果我这样输入。我喜欢这种方式。或者甚至喜欢这个。我仍然得到我的观点。

另外,查尔斯,你是我见过的第一个对我输入方式有所了解的人。除了我的Shift键获得殴打,而不是你。事实上,查尔斯在创造这一信息时受到了伤害。

有程序员想要帮助。然后那里有几年的家伙,认为他们知道这一切,并希望对待所有他们认为不像垃圾的人。请大家关注他的帖子,看看你得出的结论。