0

最新のツイートを取得していますが、結果は次のように返されます。

    [{
    "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 を試しましたが、うまくいきません。

4

2 に答える 2

0

JSON 出力を文字列に保存しようとすると、単一引用符がエスケープされていないために失敗します。

たとえば、JSON 出力に次のテキストがあります。

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

そのため、JSON を文字列に割り当てようとすると、単一引用符が含まれているために失敗し、解析エラーが発生します。JSON には二重引用符が含まれているため、それも使用できません。

引用符を無視して文字列として解釈するように PHP に指示するには、次のようにヒアドキュメント構文を使用できます。

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

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

json_decode()注: 代わりに連想配列を取得する場合は、 asの 2 番目のパラメーターを渡すことができますTRUE

動作デモ!

于 2013-08-28T12:17:12.947 に答える