PHP 来自文件类的推文

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 来自文件类的推文相关的知识,希望对你有一定的参考价值。

// (C) Copyright JAMES PADOLSEY
class tweetFromFile {
 
    private $curlHandle;
    private $updateFile;
    private $archiveFile;
 
    private function getNewStatus() {
 
        $upcomingTweetsFile = $this->files['upcoming'];
        $archivedTweetsFile = $this->files['archive'];
 
        $upcomingTweets_R = fopen($upcomingTweetsFile, "r");
 
        // Get upcoming Tweets:
        $contents = fread($upcomingTweets_R, filesize($upcomingTweetsFile));
        $splitContents = preg_split('/\n/', $contents, 2);
 
        // ARCHIVE OLD POSTS:
        $archive = fopen($archivedTweetsFile, "a");
        fwrite($archive, $splitContents[0]."\n");
 
        // Remove top line from upcoming:
        $upcomingTweets_W = fopen($upcomingTweetsFile, "w");
        fwrite($upcomingTweets_W, $splitContents[1]);
 
        // Clean up
        fclose($upcomingTweets_W);
        fclose($upcomingTweets_R);
        fclose($archive);
 
        return $splitContents[0];
    }
 
    public $files = array('upcoming' => '', 'archive' => '');
 
    public function __construct($username, $password, $filename) {
 
        $this->curlHandle = curl_init();
        $this->files['upcoming'] = $filename;
        $this->files['archive'] = 'ARCHIVE_' . $filename;
 
        // Shortcut:
        $ch = $this->curlHandle;
 
        curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/update.xml");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
 
    }
 
    public function __destruct() {
        curl_close($this->curlHandle);
    }
 
    public function updateStatus() {
 
        $status = $this->getNewStatus();
 
        curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, "status=$status");
 
        $result = curl_exec($this->curlHandle);	
	$resultArray = curl_getinfo($this->curlHandle);
 
        if ($resultArray['http_code'] == 200) return true;
 
        return false;
 
    }
 
}
 
// ==================
// ===== USAGE ======
// ==================
 
$tweet = new tweetFromFile('twitterUsername', 'password', 'textfile.txt');
$success = $tweet->updateStatus();
if ($success) {
    echo 'Twitter updated!';
} else {
    echo 'Hmm, an error...';
}

以上是关于PHP 来自文件类的推文的主要内容,如果未能解决你的问题,请参考以下文章

Python:预测来自用户的推文数量

仅使用 Twitter 流 API 显示来自单个用户的推文

使用 Twitter 流 API,是不是可以只显示来自特定用户的推文?

如何收听仅包含来自 Twitter 流的地理信息的推文

有没有办法用来自特定用户的推文触发 AWS lambda 函数

如何跟踪来自特定城市的推文并通过 python 存储在 MongoDB 中?