如何从twitter的JSON响应中访问元素?

时间:2012-10-18 14:54:49

标签: php twitter json

根据Twitter的以下回复,我如何访问'followers_count'

array(1) { 
    [0]=> object(stdClass)#1 (21) { 
        ["created_at"]=> string(30) "Mon Oct 15   19:20:28 +0000 2012" 
        ["id"]=> int(257923917879070721) 
        ["id_str"]=> string(18) "257923917879070721" 
        ["text"]=> string(127) "As open enrollment begins, Medicare is getting stronger – but Rep. Ryan wants to dismantle it. My op-ed: http://t.co/DUEYxQOt"  
        ["source"]=> string(3) "web"  
        ["truncated"]=> bool(false)  
        ["in_reply_to_status_id"]=> NULL  
        ["in_reply_to_status_id_str"]=> NULL  
        ["in_reply_to_user_id"]=> NULL  
        ["in_reply_to_user_id_str"]=> NULL  
        ["in_reply_to_screen_name"]=> NULL  
        ["user"]=> object(stdClass)#2 (38) {  
            ["id"]=> int(16789970)  
            ["id_str"]=> string(8) "16789970"  
            ["name"]=> string(18) "Senator Harry Reid"  
            ["screen_name"]=> string(11) "SenatorReid"  
            ["location"]=> string(15) "Searchlight, NV"  
            ["url"]=> string(22) "http://reid.senate.gov"  
            ["description"]=> string(108) "News from the office of Nevada Senator Harry Reid, the U.S. Senate Majority Leader. (Español: @SenadorReid)"  
            ["protected"]=> bool(false)  
            ["followers_count"]=> int(67261)  
            ["friends_count"]=> int(126)  
            ["listed_count"]=> int(3712)  
            ["created_at"]=> string(30) "Wed Oct 15 19:42:08 +0000 2008"  
            ["favourites_count"]=> int(1)  
            ["utc_offset"]=> int(-18000)  
            ["time_zone"]=> string(5) "Quito"  
            ["geo_enabled"]=> bool(false)  
            ["verified"]=> bool(true)  
            ["statuses_count"]=> int(1556)  
            ["lang"]=> string(2) "en"  
            ["contributors_enabled"]=> bool(false)  
            ["is_translator"]=> bool(false)  
            ["profile_background_color"]=> string(6) "233E6D"  
            ["profile_background_image_url"]=> string(70) "http://a0.twimg.com/profile_background_images/4744650/reid_twitter.jpg"  
            ["profile_background_image_url_https"]=> string(72) "https://si0.twimg.com/profile_background_images/4744650/reid_twitter.jpg"  
            ["profile_background_tile"]=> bool(false)  
            ["profile_image_url"]=> string(66) "http://a0.twimg.com/profile_images/81906216/reid_square_normal.jpg"  
            ["profile_image_url_https"]=> string(68) "https://si0.twimg.com/profile_images/81906216/reid_square_normal.jpg"  
            ["profile_banner_url"]=> string(63) "https://si0.twimg.com/brand_banners/SenatorReid/1323385337/live"  
            ["profile_link_color"]=> string(6) "004CB4"  
            ["profile_sidebar_border_color"]=> string(6) "6383A3"  
            ["profile_sidebar_fill_color"]=> string(6) "AFC4DA"  
            ["profile_text_color"]=> string(6) "333333"  
            ["profile_use_background_image"]=> bool(true)  
            ["default_profile"]=> bool(false)  
            ["default_profile_image"]=> bool(false)  
            ["following"]=> NULL  
            ["follow_request_sent"]=> NULL  
            ["notifications"]=> NULL } ["geo"]=> NULL  
            ["coordinates"]=> NULL  
            ["place"]=> NULL  
            ["contributors"]=> NULL  
            ["retweet_count"]=> int(88)  
            ["entities"]=> object(stdClass)#3 (3) {  
                ["hashtags"]=> array(0) { }  
                ["urls"]=> array(1) {  
                    [0]=> object(stdClass)#4 (4) {  
                        ["url"]=> string(20) "http://t.co/DUEYxQOt"  
                        ["expanded_url"]=> string(21) "http://huff.to/TshePO"  
                        ["display_url"]=> string(14) "huff.to/TshePO"  
                        ["indices"]=> array(2) {  
                            [0]=> int(105)  
                            [1]=> int(125) 
                        } 
                } 
            } 
            ["user_mentions"]=> array(0) { } 
        } 
        ["favorited"]=> bool(false) 
        ["retweeted"]=> bool(false) 
        ["possibly_sensitive"]=> bool(false) 
    } 
}

2 个答案:

答案 0 :(得分:3)

要轻松阅读var_dump,可以安装x-debug:http://xdebug.org/

要访问您的信息,请访问:

$followersCount = $twitterResponse[0]->user->followers_count

为你的例子。

答案 1 :(得分:0)

$response[0]["user"]["followers_count"]