php - Twitter - get a specific tweet from returned data? -


i'm getting latest tweets , results come so:

    [{     "created_at": "wed aug 28 11:50:22 +0000 2013",     "id": 372687598952919040,     "id_str": "372687598952919040",     "text": "\"i fear we'll ourselves involved in 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 , analysis bbc news (uk edition). world edition, follow @bbcworld. 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 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 , analysis bbc news (uk edition). world edition, follow @bbcworld. 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" }] 

how can use can cherry pick tweets array, eg. $tweets[2]; i've tried json_decode no luck.

if try store json output in string, fail due unescaped single quotes in it.

for example, have following text in json output:

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

so, if try assign json string, fail , produce parse error because of single quotes in it. because json contains double-quotes, can't use that, either.

to tell php ignore quotes , interpret string, can use heredoc syntax, so:

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

note: if want associative array instead, can pass the second paramter json_decode() true.

working demo!


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -