我想将搜索后发现的推文存储在文本文件中


I want to store tweets that I find after a search, in a text file

有人知道我在.txt文件中搜索后如何保存推文吗?我的索引文件如下:

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <title>Twitter</title>
     </head>
     <body>
  <?php
     require('twitter.class.php');
     $twitter = new twitter_class();
     echo $twitter->getTweets('tomato', 15);
   ?>
   </body>
   </html>

我对这一切都是新手,所以如果有任何帮助,我将不胜感激。

以下是保存推文的代码:

<?php
    require('twitter.class.php');
    $twitter = new twitter_class();
    $tweets = $twitter->getTweets('tomato', 15);
    $currentfile = file_get_contents('tweets.txt');
    file_put_contents('tweets.txt', $currentfile.$tweets);
?>

这将附加推文,而不是删除数据,如果你不想附加推文只需这样做:

<?php
    require('twitter.class.php');
    $twitter = new twitter_class();
    $tweets = $twitter->getTweets('tomato', 15);
    file_put_contents('tweets.txt', $tweets);
?>

fwrite()是您的朋友。在循环中,您回显tweets而不是回显它,将它们写入文本文件

您尝试过函数file_punt_contents吗?

你可以做:

<?php
    $file = 'tweets.txt';
    $tweets = $twitter->getTweets('tomato', 15);
    // Write the contents back to the file
    file_put_contents($file, $tweets);
?>

更多信息。

您可以在php-usine fwrite中创建文件并写入,这里有一个简单的代码:

 $fp = fopen('data.txt', 'w');
    fwrite($fp, $twitter->getTweets('tomato', 15););
    fclose($fp);