我实际上正在开发一个小型推特项目。为此我只需要根据搜索将状态,即推文保存到.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.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"
}
}
我只想要&#39;其余的以后做FIFA&#39;来自&#34; text&#34;:&#34;在上面的FIFA&#34;以及其余部分的其余部分中的searchtweets.txt。
答案 0 :(得分: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
中