2014-07-21 5 views
0

실제로 작은 트위터 프로젝트에서 일하고 있습니다. 검색에 따라 트윗을 .txt 파일로 저장하는 상태 즉, 상태 만 있으면됩니다. 지금은 내 검색의 json 형식을 .txt 파일에 저장하는 것만 가능합니다. 전에 json으로 일한 적이 없으므로 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.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" 
    } 
} 

난 단지 "텍스트"에서 searchtweets.txt에서 '나머지 나중에 FIFA를 수행합니다'원하는 : "나중에 나머지를 수행 FIFA "라고합니다.

답변

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에만 트윗을 저장할 수 있습니다.

관련 문제