Twitter - 从返回的数据中获取特定的推文?

时间:2013-08-28 12:06:09

标签: php twitter

我收到了最新的推文,结果如下:

    [{
    "created_at": "Wed Aug 28 11:50:22 +0000 2013",
    "id": 372687598952919040,
    "id_str": "372687598952919040",
    "text": "\"I fear we'll get ourselves involved in another endless conflict\" - Ukip's @Nigel_Farage on UK's #Syria decision http:\/\/t.co\/3iMlO9BHks",
    "source": "<a href=\"http:\/\/www.bbc.co.uk\/news\/\" rel=\"nofollow\">BBC News<\/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": 612473,
        "id_str": "612473",
        "name": "BBC News (UK)",
        "screen_name": "BBCNews",
        "location": "London",
        "description": "News, features and analysis from BBC News (UK edition). For world edition, follow @BBCWorld. For breaking news, follow @BBCBreaking. Latest sport news @BBCSport",
        "url": "http:\/\/t.co\/2XmxtfHbzW",
        "entities": {
            "url": {
                "urls": [{
                    "url": "http:\/\/t.co\/2XmxtfHbzW",
                    "expanded_url": "http:\/\/www.bbc.co.uk\/news",
                    "display_url": "bbc.co.uk\/news",
                    "indices": [0, 22]
                }]
            },
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 1886892,
        "friends_count": 91,
        "listed_count": 24941,
        "created_at": "Mon Jan 08 08:05:57 +0000 2007",
        "favourites_count": 0,
        "utc_offset": 3600,
        "time_zone": "London",
        "geo_enabled": false,
        "verified": true,
        "statuses_count": 190164,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "profile_background_color": "FFFFFF",
        "profile_background_image_url": "http:\/\/a0.twimg.com\/profile_background_images\/571083158\/l7tnxw8xy058c4rm6e4f.jpeg",
        "profile_background_image_url_https": "https:\/\/si0.twimg.com\/profile_background_images\/571083158\/l7tnxw8xy058c4rm6e4f.jpeg",
        "profile_background_tile": false,
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2186831845\/128x128_twitter_bbc_uk_normal.jpg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2186831845\/128x128_twitter_bbc_uk_normal.jpg",
        "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/612473\/1357232373",
        "profile_link_color": "1F527B",
        "profile_sidebar_border_color": "CCCCCC",
        "profile_sidebar_fill_color": "FFFFFF",
        "profile_text_color": "5A5A5A",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": null,
        "follow_request_sent": false,
        "notifications": null
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 28,
    "favorite_count": 12,
    "entities": {
        "hashtags": [{
            "text": "Syria",
            "indices": [97, 103]
        }],
        "symbols": [],
        "urls": [{
            "url": "http:\/\/t.co\/3iMlO9BHks",
            "expanded_url": "http:\/\/bbc.in\/13YTz5f",
            "display_url": "bbc.in\/13YTz5f",
            "indices": [113, 135]
        }],
        "user_mentions": [{
            "screen_name": "Nigel_Farage",
            "name": "Nigel Farage",
            "id": 19017675,
            "id_str": "19017675",
            "indices": [75, 88]
        }]
    },
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
}, {
    "created_at": "Wed Aug 28 11:27:11 +0000 2013",
    "id": 372681763640061952,
    "id_str": "372681763640061952",
    "text": "International agreement, legal basis, support in country - @HarrietHarman on criteria needed to support Syria action http:\/\/t.co\/I5fWeyt9gl",
    "source": "<a href=\"http:\/\/www.bbc.co.uk\/news\/\" rel=\"nofollow\">BBC News<\/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": 612473,
        "id_str": "612473",
        "name": "BBC News (UK)",
        "screen_name": "BBCNews",
        "location": "London",
        "description": "News, features and analysis from BBC News (UK edition). For world edition, follow @BBCWorld. For breaking news, follow @BBCBreaking. Latest sport news @BBCSport",
        "url": "http:\/\/t.co\/2XmxtfHbzW",
        "entities": {
            "url": {
                "urls": [{
                    "url": "http:\/\/t.co\/2XmxtfHbzW",
                    "expanded_url": "http:\/\/www.bbc.co.uk\/news",
                    "display_url": "bbc.co.uk\/news",
                    "indices": [0, 22]
                }]
            },
            "description": {
                "urls": []
            }
        },
        "protected": false,
        "followers_count": 1886892,
        "friends_count": 91,
        "listed_count": 24941,
        "created_at": "Mon Jan 08 08:05:57 +0000 2007",
        "favourites_count": 0,
        "utc_offset": 3600,
        "time_zone": "London",
        "geo_enabled": false,
        "verified": true,
        "statuses_count": 190164,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "profile_background_color": "FFFFFF",
        "profile_background_image_url": "http:\/\/a0.twimg.com\/profile_background_images\/571083158\/l7tnxw8xy058c4rm6e4f.jpeg",
        "profile_background_image_url_https": "https:\/\/si0.twimg.com\/profile_background_images\/571083158\/l7tnxw8xy058c4rm6e4f.jpeg",
        "profile_background_tile": false,
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2186831845\/128x128_twitter_bbc_uk_normal.jpg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2186831845\/128x128_twitter_bbc_uk_normal.jpg",
        "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/612473\/1357232373",
        "profile_link_color": "1F527B",
        "profile_sidebar_border_color": "CCCCCC",
        "profile_sidebar_fill_color": "FFFFFF",
        "profile_text_color": "5A5A5A",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": null,
        "follow_request_sent": false,
        "notifications": null
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 17,
    "favorite_count": 4,
    "entities": {
        "hashtags": [],
        "symbols": [],
        "urls": [{
            "url": "http:\/\/t.co\/I5fWeyt9gl",
            "expanded_url": "http:\/\/bbc.in\/13YQD8O",
            "display_url": "bbc.in\/13YQD8O",
            "indices": [117, 139]
        }],
        "user_mentions": [{
            "screen_name": "HarrietHarman",
            "name": "Harriet Harman",
            "id": 19977759,
            "id_str": "19977759",
            "indices": [59, 73]
        }]
    },
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
}]

我如何使用它,以便我可以从阵列中挑选推文,例如。 $tweets[2];我尝试了json_decode,但没有运气。

2 个答案:

答案 0 :(得分:1)

您可以尝试将第二个参数传递给json_decode,它会将所有json数据转储为关联数组 link

答案 1 :(得分:0)

如果您尝试将JSON输出存储在字符串中,则由于其中未转义的单引号,它将失败。

例如,JSON输出中包含以下文本:

"text": "\"I fear we'll get ourselves involved in another endless conflict\" 
            //------^

因此,如果您尝试将JSON分配给字符串,它将失败并产生解析错误,因为它中包含单引号。因为您的JSON包含双引号,所以您也不能使用它。

要告诉PHP忽略引号并将其解释为字符串,您可以使用heredoc语法,如下所示:

$string = <<<JSON
... json ...
JSON;

$json = json_decode($string);
echo $json[0]->text."\n"; 
echo $json[1]->text."\n";

注意:如果您想要获得关联数组,可以将json_decode()的第二个参数传递为TRUE

Working demo!