2014-07-21 38 views
0

我實際上在一個小型的twitter項目上工作。爲此,我只需要根據搜索情況將推文保存到.txt文件中。現在,我只能將搜索的json表單保存到.txt文件中。對於json,我從來沒有使用過它,所以不太好。任何幫助深表感謝。Twitter只是文本(狀態/鳴叫)

<?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.9 
       ], 
       [ 
       1.7689359995018, 
       51.9 
       ], 
       [ 
       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" 
    } 
} 

我只想「做剩下的以後FIFA」從「文」 searchtweets.txt:「以後做休息國際足聯「,以上。

回答

0

我解決了我在找的東西。

$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文件中