我实际上在做一个小小的twitter项目。为此,我需要的只是状态,即tweets被保存到一个基于搜索。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"
}
}
我只想在"text":"Do the rest later FIFA"的searchtweets.txt中输入"Do the rest later 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);
this已经完成了。现在我可以只保存tweets到我的。txt