Twitter Rest API,试图解析数据

时间:2014-09-04 18:59:23

标签: php json twitter json-api

我试图获取用户Feed的所有图片。我创建的文件是CRON作业,但会检查数据库中是否有已提交的用户名,然后转到Twitter API并抓取用户的订阅源,然后特别是图像。我遇到的问题是我似乎无法浏览回复。目前我使用json_decode(TWITTERS RESPONSE HERE),此后我无法进入低级别"数据的。我的代码如下:

//Loop through each user with twitter and lets get their feed
while( $user = mysqli_fetch_assoc($user_twitter_query) ){

    //Lets get our twitter API call ready
    $twitter_url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
    $twitter_getfields = '?screen_name='.$user[twitter].'&count=20&include_rts=0&filter:images';
    $twitter_requestMethod = 'GET';

    $twitter = new TwitterAPIExchange($twitter_settings);   
    $response = $twitter->setGetfield($twitter_getfields)
         ->buildOauth($twitter_url, $twitter_requestMethod) 
             ->performRequest();
    //echo $response;

    $data = json_decode($response);

    if(!empty($data)){
        $twitter_posts = $data->entities;

        if(!empty($twitter_posts)){

            foreach($twitter_posts->media AS $media_element){   
                $media = $media_element->media_url;

                if(!empty($media)){
                    foreach ($media as $img) {

                        echo $img;
                    }
                }
            }   
        }
    }
}

进行" var转储"在$ data上(我将api调用更改为" count = 1"以节省它包含的空间:

array(1) { [0]=> object(stdClass)#5 (23) { ["created_at"]=> string(30) "Thu Sep 04 19:14:03 +0000 2014" ["id"]=> int(507607543678767104) ["id_str"]=> string(18) "507607543678767104" ["text"]=> string(70) "A look back at @Joan_Rivers' extraordinary life http://t.co/0C4a1cPJVh" ["source"]=> string(56) "Buffer" ["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)#6 (40) { ["id"]=> int(26371180) ["id_str"]=> string(8) "26371180" ["name"]=> string(9) "About.com" ["screen_name"]=> string(11) "aboutdotcom" ["location"]=> string(13) "New York City" ["description"]=> string(160) "More than 900 sites on everything from parenting and healthcare to technology, cooking & travel. (Photo: Gardening with your kids tips! http://t.co/jYjsRKhCD4)" ["url"]=> string(22) "http://t.co/WbgBQpP00O" ["entities"]=> object(stdClass)#7 (2) { ["url"]=> object(stdClass)#8 (1) { ["urls"]=> array(1) { [0]=> object(stdClass)#9 (4) { ["url"]=> string(22) "http://t.co/WbgBQpP00O" ["expanded_url"]=> string(20) "http://www.about.com" ["display_url"]=> string(9) "about.com" ["indices"]=> array(2) { [0]=> int(0) [1]=> int(22) } } } } ["description"]=> object(stdClass)#10 (1) { ["urls"]=> array(1) { [0]=> object(stdClass)#11 (4) { ["url"]=> string(22) "http://t.co/jYjsRKhCD4" ["expanded_url"]=> string(21) "http://abt.cm/1pzih31" ["display_url"]=> string(14) "abt.cm/1pzih31" ["indices"]=> array(2) { [0]=> int(137) [1]=> int(159) } } } } } ["protected"]=> bool(false) ["followers_count"]=> int(20657) ["friends_count"]=> int(2018) ["listed_count"]=> int(544) ["created_at"]=> string(30) "Wed Mar 25 00:22:20 +0000 2009" ["favourites_count"]=> int(1395) ["utc_offset"]=> int(-14400) ["time_zone"]=> string(26) "Eastern Time (US & Canada)" ["geo_enabled"]=> bool(true) ["verified"]=> bool(true) ["statuses_count"]=> int(11719) ["lang"]=> string(2) "en" ["contributors_enabled"]=> bool(false) ["is_translator"]=> bool(false) ["is_translation_enabled"]=> bool(false) ["profile_background_color"]=> string(6) "FFFFFF" ["profile_background_image_url"]=> string(82) "http://pbs.twimg.com/profile_background_images/593914807/ifedvqabuwiyg9aj6bhj.jpeg" ["profile_background_image_url_https"]=> string(83) "https://pbs.twimg.com/profile_background_images/593914807/ifedvqabuwiyg9aj6bhj.jpeg" ["profile_background_tile"]=> bool(true) ["profile_image_url"]=> string(75) "http://pbs.twimg.com/profile_images/506775843595489280/UZxND16z_normal.jpeg" ["profile_image_url_https"]=> string(76) "https://pbs.twimg.com/profile_images/506775843595489280/UZxND16z_normal.jpeg" ["profile_banner_url"]=> string(57) "https://pbs.twimg.com/profile_banners/26371180/1409669959" ["profile_link_color"]=> string(6) "FB0404" ["profile_sidebar_border_color"]=> string(6) "FEFCFC" ["profile_sidebar_fill_color"]=> string(6) "FFFFFF" ["profile_text_color"]=> string(6) "1C1F23" ["profile_use_background_image"]=> bool(true) ["default_profile"]=> bool(false) ["default_profile_image"]=> bool(false) ["following"]=> bool(true) ["follow_request_sent"]=> bool(false) ["notifications"]=> bool(false) } ["geo"]=> NULL ["coordinates"]=> NULL ["place"]=> NULL ["contributors"]=> NULL ["retweet_count"]=> int(0) ["favorite_count"]=> int(0) ["entities"]=> object(stdClass)#12 (4) { ["hashtags"]=> array(0) { } ["symbols"]=> array(0) { } ["urls"]=> array(1) { [0]=> object(stdClass)#13 (4) { ["url"]=> string(22) "http://t.co/0C4a1cPJVh" ["expanded_url"]=> string(20) "http://abt.cm/Wh1Jmx" ["display_url"]=> string(13) "abt.cm/Wh1Jmx" ["indices"]=> array(2) { [0]=> int(48) [1]=> int(70) } } } ["user_mentions"]=> array(1) { [0]=> object(stdClass)#14 (5) { ["screen_name"]=> string(11) "Joan_Rivers" ["name"]=> string(11) "Joan Rivers" ["id"]=> int(21414413) ["id_str"]=> string(8) "21414413" ["indices"]=> array(2) { [0]=> int(15) [1]=> int(27) } } } } ["favorited"]=> bool(false) ["retweeted"]=> bool(false) ["possibly_sensitive"]=> bool(false) ["lang"]=> string(2) "en" } }

以下是实际API调用返回的内容(这有20个结果):

[
{
    "created_at": "Thu Sep 04 19:20:10 +0000 2014",
    "id": 507609079179517950,
    "id_str": "507609079179517954",
    "text": "Cowen and Company Global Transportation & Aerospace/Defense Conference Webcast: More Info http://t.co/W1vYCmLVYT",
    "source": "<a href=\"http://twitterfeed.com\" rel=\"nofollow\">twitterfeed</a>",
    "truncated": 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": {
        "id": 269930038,
        "id_str": "269930038",
        "name": "Phoenix Consulting",
        "screen_name": "phoenix_cc",
        "location": "Greater Chicagoland",
        "description": "Phoenix Consulting Corp. has been\r\nproviding Small Businesses with affordable Information Technology solutions since 2000.",
        "url": "http://t.co/9iMwFBT6Qb",
        "entities": {
            "url": {
                "urls": [
                    {
                        "url": "http://t.co/9iMwFBT6Qb",
                        "expanded_url": "http://www.phoenix-cc.com",
                        "display_url": "phoenix-cc.com",
                        "indices": [
                            0,
                            22
                        ]
                    }
                ]
            },
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 53414,
        "friends_count": 54028,
        "listed_count": 220,
        "created_at": "Mon Mar 21 18:19:09 +0000 2011",
        "favourites_count": 0,
        "utc_offset": -18000,
        "time_zone": "Central Time (US & Canada)",
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 32398,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "020812",
        "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/698810400/6e05a48d548fcc6a2ebda6658d7ea447.jpeg",
        "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/698810400/6e05a48d548fcc6a2ebda6658d7ea447.jpeg",
        "profile_background_tile": false,
        "profile_image_url": "http://pbs.twimg.com/profile_images/2479870538/4rihbthznzcefj66axaa_normal.png",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/2479870538/4rihbthznzcefj66axaa_normal.png",
        "profile_banner_url": "https://pbs.twimg.com/profile_banners/269930038/1408286715",
        "profile_link_color": "17406C",
        "profile_sidebar_border_color": "000000",
        "profile_sidebar_fill_color": "020812",
        "profile_text_color": "2280A9",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": true,
        "follow_request_sent": false,
        "notifications": false
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 0,
    "favorite_count": 0,
    "entities": {
        "hashtags": [],
        "symbols": [],
        "urls": [
            {
                "url": "http://t.co/W1vYCmLVYT",
                "expanded_url": "http://bit.ly/1ryJ9Tc",
                "display_url": "bit.ly/1ryJ9Tc",
                "indices": [
                    94,
                    116
                ]
            }
        ],
        "user_mentions": []
    },
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
},
{
    "created_at": "Thu Sep 04 19:20:01 +0000 2014",
    "id": 507609041829236740,
    "id_str": "507609041829236737",
    "text": "There’s going to be great football this season. Get @NFLRedZone from @NFLNetwork w/ TWC Sports Pass: http://t.co/MA48RMg5fy",
    "source": "<a href=\"http://www.wildfireapp.com/?utm_source=Twitter&utm_medium=Tweet&utm_campaign=via%2BWildfire%2BSuite\" rel=\"nofollow\">Wildfire Suite</a>",
    "truncated": 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": {
        "id": 127975950,
        "id_str": "127975950",
        "name": "Time Warner Cable",
        "screen_name": "TWC",
        "location": "New York, NY",
        "description": "Sports, Movies, Apps, Gaming. Whatever you're passionate about, we invent better ways to love it even more. Tweet @TWC_Help for support.",
        "url": "http://t.co/q11A003LM6",
        "entities": {
            "url": {
                "urls": [
                    {
                        "url": "http://t.co/q11A003LM6",
                        "expanded_url": "http://twc.com",
                        "display_url": "twc.com",
                        "indices": [
                            0,
                            22
                        ]
                    }
                ]
            },
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 84207,
        "friends_count": 1417,
        "listed_count": 628,
        "created_at": "Tue Mar 30 19:22:54 +0000 2010",
        "favourites_count": 3518,
        "utc_offset": -14400,
        "time_zone": "Eastern Time (US & Canada)",
        "geo_enabled": true,
        "verified": true,
        "statuses_count": 20269,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "131516",
        "profile_background_image_url": "http://abs.twimg.com/images/themes/theme14/bg.gif",
        "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme14/bg.gif",
        "profile_background_tile": true,
        "profile_image_url": "http://pbs.twimg.com/profile_images/454463073168789504/NGDbfqlF_normal.jpeg",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/454463073168789504/NGDbfqlF_normal.jpeg",
        "profile_banner_url": "https://pbs.twimg.com/profile_banners/127975950/1409664187",
        "profile_link_color": "009999",
        "profile_sidebar_border_color": "EEEEEE",
        "profile_sidebar_fill_color": "EFEFEF",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": true,
        "follow_request_sent": false,
        "notifications": false
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 0,
    "favorite_count": 1,
    "entities": {
        "hashtags": [],
        "symbols": [],
        "urls": [
            {
                "url": "http://t.co/MA48RMg5fy",
                "expanded_url": "http://bit.ly/TWCSportsPass",
                "display_url": "bit.ly/TWCSportsPass",
                "indices": [
                    101,
                    123
                ]
            }
        ],
        "user_mentions": [
            {
                "screen_name": "nflredzone",
                "name": "NFL RedZone",
                "id": 67133275,
                "id_str": "67133275",
                "indices": [
                    52,
                    63
                ]
            },
            {
                "screen_name": "nflnetwork",
                "name": "NFL Network",
                "id": 19362299,
                "id_str": "19362299",
                "indices": [
                    69,
                    80
                ]
            }
        ]
    },
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
},
{
    "created_at": "Thu Sep 04 19:20:01 +0000 2014",
    "id": 507609041720582140,
    "id_str": "507609041720582144",
    "text": "The #GalaxyNote4 is coming soon to @ATT, @Sprint, @TMobile, @VerizonWireless and @USCellular. http://t.co/M6FexsCCH7 http://t.co/WNCKli0wQr",
    "source": "<a href=\"http://www.sprinklr.com\" rel=\"nofollow\">Sprinklr</a>",
    "truncated": 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": {
        "id": 14117843,
        "id_str": "14117843",
        "name": "Samsung Mobile US",
        "screen_name": "SamsungMobileUS",
        "location": "USA",
        "description": "Official tweets from #TheNextBigThing. For device assistance, say hello to @SamsungSupport.",
        "url": "http://t.co/wuId6XmZ05",
        "entities": {
            "url": {
                "urls": [
                    {
                        "url": "http://t.co/wuId6XmZ05",
                        "expanded_url": "http://www.facebook.com/SamsungMobileUSA",
                        "display_url": "facebook.com/SamsungMobileU…",
                        "indices": [
                            0,
                            22
                        ]
                    }
                ]
            },
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 5282237,
        "friends_count": 21375,
        "listed_count": 8439,
        "created_at": "Mon Mar 10 21:49:36 +0000 2008",
        "favourites_count": 52784,
        "utc_offset": -18000,
        "time_zone": "Central Time (US & Canada)",
        "geo_enabled": false,
        "verified": true,
        "statuses_count": 82823,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "292B2D",
        "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/482684612582182913/GevPW6aH.jpeg",
        "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/482684612582182913/GevPW6aH.jpeg",
        "profile_background_tile": false,
        "profile_image_url": "http://pbs.twimg.com/profile_images/454497647886213120/nhEnP9E9_normal.jpeg",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/454497647886213120/nhEnP9E9_normal.jpeg",
        "profile_banner_url": "https://pbs.twimg.com/profile_banners/14117843/1409752013",
        "profile_link_color": "0084B4",
        "profile_sidebar_border_color": "000000",
        "profile_sidebar_fill_color": "C0DFEC",
        "profile_text_color": "333333",
        "profile_use_background_image": false,
        "default_profile": false,
        "default_profile_image": false,
        "following": true,
        "follow_request_sent": false,
        "notifications": false
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 1,
    "favorite_count": 1,
    "entities": {
        "hashtags": [
            {
                "text": "GalaxyNote4",
                "indices": [
                    4,
                    16
                ]
            }
        ],
        "symbols": [],
        "urls": [
            {
                "url": "http://t.co/M6FexsCCH7",
                "expanded_url": "http://smsng.us/note4",
                "display_url": "smsng.us/note4",
                "indices": [
                    94,
                    116
                ]
            }
        ],
        "user_mentions": [
            {
                "screen_name": "ATT",
                "name": "AT&T",
                "id": 14620190,
                "id_str": "14620190",
                "indices": [
                    35,
                    39
                ]
            },
            {
                "screen_name": "sprint",
                "name": "Sprint",
                "id": 14626738,
                "id_str": "14626738",
                "indices": [
                    41,
                    48
                ]
            },
            {
                "screen_name": "TMobile",
                "name": "T-Mobile",
                "id": 17338082,
                "id_str": "17338082",
                "indices": [
                    50,
                    58
                ]
            },
            {
                "screen_name": "VerizonWireless",
                "name": "Verizon Wireless USA",
                "id": 59889953,
                "id_str": "59889953",
                "indices": [
                    60,
                    76
                ]
            },
            {
                "screen_name": "USCellular",
                "name": "U.S. Cellular ",
                "id": 274538956,
                "id_str": "274538956",
                "indices": [
                    81,
                    92
                ]
            }
        ],
        "media": [
            {
                "id": 507609041586384900,
                "id_str": "507609041586384896",
                "indices": [
                    117,
                    139
                ],
                "media_url": "http://pbs.twimg.com/media/Bwtju-CIcAACoZE.jpg",
                "media_url_https": "https://pbs.twimg.com/media/Bwtju-CIcAACoZE.jpg",
                "url": "http://t.co/WNCKli0wQr",
                "display_url": "pic.twitter.com/WNCKli0wQr",
                "expanded_url": "http://twitter.com/SamsungMobileUS/status/507609041720582144/photo/1",
                "type": "photo",
                "sizes": {
                    "small": {
                        "w": 340,
                        "h": 170,
                        "resize": "fit"
                    },
                    "thumb": {
                        "w": 150,
                        "h": 150,
                        "resize": "crop"
                    },
                    "medium": {
                        "w": 600,
                        "h": 300,
                        "resize": "fit"
                    },
                    "large": {
                        "w": 960,
                        "h": 480,
                        "resize": "fit"
                    }
                }
            }
        ]
    },
    "extended_entities": {
        "media": [
            {
                "id": 507609041586384900,
                "id_str": "507609041586384896",
                "indices": [
                    117,
                    139
                ],
                "media_url": "http://pbs.twimg.com/media/Bwtju-CIcAACoZE.jpg",
                "media_url_https": "https://pbs.twimg.com/media/Bwtju-CIcAACoZE.jpg",
                "url": "http://t.co/WNCKli0wQr",
                "display_url": "pic.twitter.com/WNCKli0wQr",
                "expanded_url": "http://twitter.com/SamsungMobileUS/status/507609041720582144/photo/1",
                "type": "photo",
                "sizes": {
                    "small": {
                        "w": 340,
                        "h": 170,
                        "resize": "fit"
                    },
                    "thumb": {
                        "w": 150,
                        "h": 150,
                        "resize": "crop"
                    },
                    "medium": {
                        "w": 600,
                        "h": 300,
                        "resize": "fit"
                    },
                    "large": {
                        "w": 960,
                        "h": 480,
                        "resize": "fit"
                    }
                }
            }
        ]
    },
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
},
{
    "created_at": "Thu Sep 04 19:16:53 +0000 2014",
    "id": 507608256521310200,
    "id_str": "507608256521310209",
    "text": "E-cigarettes a 'gateway' to harder drugs, study says: http://t.co/vJCY6AaDX6",
    "source": "<a href=\"http://twitterfeed.com\" rel=\"nofollow\">twitterfeed</a>",
    "truncated": 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": {
        "id": 407473142,
        "id_str": "407473142",
        "name": "Royal W Johnson",
        "screen_name": "RoyalWJohnson",
        "location": "",
        "description": "B2B CFO® Partner helping business owners reduce stress and cash flow worries.\r\n#part-time cfo,#cfo, #fractionalcfo",
        "url": "http://t.co/CrCojXBz2j",
        "entities": {
            "url": {
                "urls": [
                    {
                        "url": "http://t.co/CrCojXBz2j",
                        "expanded_url": "http://www.b2bcfo.com/partners/rjohnson",
                        "display_url": "b2bcfo.com/partners/rjohn…",
                        "indices": [
                            0,
                            22
                        ]
                    }
                ]
            },
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 83168,
        "friends_count": 84354,
        "listed_count": 545,
        "created_at": "Tue Nov 08 03:51:30 +0000 2011",
        "favourites_count": 9,
        "utc_offset": -18000,
        "time_zone": "Central Time (US & Canada)",
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 117740,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "C0DEED",
        "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_tile": false,
        "profile_image_url": "http://pbs.twimg.com/profile_images/1678439501/141_Web_normal.jpg",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/1678439501/141_Web_normal.jpg",
        "profile_link_color": "0084B4",
        "profile_sidebar_border_color": "C0DEED",
        "profile_sidebar_fill_color": "DDEEF6",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": true,
        "default_profile_image": false,
        "following": true,
        "follow_request_sent": false,
        "notifications": false
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 0,
    "favorite_count": 0,
    "entities": {
        "hashtags": [],
        "symbols": [],
        "urls": [
            {
                "url": "http://t.co/vJCY6AaDX6",
                "expanded_url": "http://fxn.ws/1vSfp6Z",
                "display_url": "fxn.ws/1vSfp6Z",
                "indices": [
                    55,
                    77
                ]
            }
        ],
        "user_mentions": []
    },
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
}

0 个答案:

没有答案