簡體   English   中英

Twitter Just Text(狀態/ tweet)

[英]Twitter Just Text(status/tweet)

我實際上正在從事一個小型Twitter項目。 為此,我只需要狀態即可,即根據搜索將推文保存到.txt文件中。 現在只能將搜索的json格式保存到.txt文件中。 json不好用,因為我以前從未使用過。 任何幫助深表感謝。

<?php
if ( isset( $tweets->errors[0]->code )) {
    // If errors exist, print the first error for a simple notification.
    echo "Error encountered: ".$tweets->errors[0]->message." Response code:" .$tweets->errors[0]->code;
 }else {
    // No errors exist. Write tweets to json/txt file.
    $tweets = $twitter->get('https://api.twitter.com/1.1/search/tweets.json?q='.$_POST['keyword'].'&lang=en&result_type=recent&count=2');
    $file = "searchtweets.txt";
    $fh = fopen($file, 'w') or die("can't open file");
    fwrite($fh, json_encode($tweets));
    fclose($fh);


}
?>

正在searchtweets.txt文件中獲取以下內容:

{
  "statuses": [
    {
      "metadata": {
        "result_type": "recent",
        "iso_language_code": "en"
      },
      "created_at": "Mon Jul 21 18:17:25 +0000 2014",
      "id": 4.912858338579e+17,
      "id_str": "491285833857904641",
      "text": "Do the rest later FIFA",
      "source": "<a href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\">Twitter for iPhone<\/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": 1523422004,
        "id_str": "1523422004",
        "name": "Mathers",
        "screen_name": "MichaelGardnerr",
        "location": "",
        "description": "",
        "url": null,
        "entities": {
          "description": {
            "urls": [

            ]
          }
        },
        "protected": false,
        "followers_count": 555,
        "friends_count": 510,
        "listed_count": 0,
        "created_at": "Sun Jun 16 22:56:26 +0000 2013",
        "favourites_count": 5932,
        "utc_offset": null,
        "time_zone": null,
        "geo_enabled": true,
        "verified": false,
        "statuses_count": 7567,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "131516",
        "profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/378800000161948327\/NcME2CFH.jpeg",
        "profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/378800000161948327\/NcME2CFH.jpeg",
        "profile_background_tile": true,
        "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/486264708174209024\/WitFpexv_normal.jpeg",
        "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/486264708174209024\/WitFpexv_normal.jpeg",
        "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1523422004\/1401822572",
        "profile_link_color": "009999",
        "profile_sidebar_border_color": "FFFFFF",
        "profile_sidebar_fill_color": "DDEEF6",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": false,
        "follow_request_sent": false,
        "notifications": false
      },
      "geo": {
        "type": "Point",
        "coordinates": [
          51.93107788,
          0.20016287
        ]
      },
      "coordinates": {
        "type": "Point",
        "coordinates": [
          0.20016287,
          51.93107788
        ]
      },
      "place": {
        "id": "3bc1b6cfd27ef7f6",
        "url": "https:\/\/api.twitter.com\/1.1\/geo\/id\/3bc1b6cfd27ef7f6.json",
        "place_type": "admin",
        "name": "East",
        "full_name": "East, United Kingdom",
        "country_code": "GB",
        "country": "United Kingdom",
        "contained_within": [

        ],
        "bounding_box": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                -0.74577980081805,
                51.448222132109
              ],
              [
                1.7689359995018,
                51.448222132109
              ],
              [
                1.7689359995018,
                52.992678957625
              ],
              [
                -0.74577980081805,
                52.992678957625
              ]
            ]
          ]
        },
        "attributes": {

        }
      },
      "contributors": null,
      "retweet_count": 0,
      "favorite_count": 0,
      "entities": {
        "hashtags": [

        ],
        "symbols": [

        ],
        "urls": [

        ],
        "user_mentions": [

        ]
      },
      "favorited": false,
      "retweeted": false,
      "lang": "en"
    }
  ],
  "search_metadata": {
    "completed_in": 0.008,
    "max_id": 4.912858338579e+17,
    "max_id_str": "491285833857904641",
    "next_results": "?max_id=491285833857904640&q=fifa&lang=en&count=1&include_entities=1&result_type=recent",
    "query": "fifa",
    "refresh_url": "?since_id=491285833857904641&q=fifa&lang=en&result_type=recent&include_entities=1",
    "count": 1,
    "since_id": 0,
    "since_id_str": "0"
  }
}

我只想從上方的“文本”中的searchtweets.txt中“稍后再做FIFA”:上面的“稍后再做FIFA”。

我已經解決了我要找的東西。

$fh = fopen($file, 'w') or die("can't open file");
    foreach($tweets as $tweet){
        foreach($tweet as $t){
            $output = $t->text;
            fwrite($fh, $output);

做到了。 現在,我只能將這些推文保存到我的.txt文件中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM