📜 ⬆️ ⬇️

Download the latest post from a specific user's Twitter blog.

I wrote a small script that receives the last message from the specified user's Twitter blog. It has a caching function - it saves the message in a text file, which avoids the need to download and process microblog RSS feeds every time. After a certain period of time, the cache is updated from the web.

How does it work


Wrote this script for one of the developed projects. The principle is simple: the RSS feed of the user you need is loaded, the last entry is selected and, if necessary, cached in a text file. XML is processed through a DOMDocument .

The public $cache_file specifies the location of the cache file.

The public $cache_period is responsible for the cache refresh rate. Indicated in seconds ( 3600 corresponds to 1 hour ). If the value is 0, the cache is ignored .
')
Everything else, I think, is understandable. If you have questions, ask.

Package Code



class GetLastTwitt{

public $cache_file = './last_twitt.txt';
public $cache_period = 3600;

private $username;
private $dom;

function __construct($username){
$this->username = $username;
}

private function setEnv(){
$feed_url = 'http://twitter.com/statuses/user_timeline/'.$this->username.'.rss';

$this->dom = new DOMDocument();
$this->dom->load($feed_url);
}

private function returnLastTwitt (){
if ($this->cache_period != 0)
if (file_exists($this->cache_file))
if ($this->cache_period > $this->getCacheDateDiff())
return $this->getLastFromCache();

return $this->getLastFromWeb($this->username);
}

private function getLastFromWeb($username){
$this->setEnv();
$rows = $this->dom->getElementsByTagName('item');
$last_twitt = $rows->item(0)->getElementsByTagName('title')->item(0)->nodeValue;
$this->cache_twitt($last_twitt);
return $last_twitt;
}

private function cache_twitt($msg){
$handle = fopen($this->cache_file,'w');
fwrite($handle, $msg);
fclose($handle);
}

private function getCacheDateDiff(){
return date('U') — filemtime($this->cache_file);
}

private function getLastFromCache(){
$handle = fopen($this->cache_file,'r');
$cached_twitt = fread($handle, filesize($this->cache_file));
fclose($handle);
return $cached_twitt;
}

final function getLast(){
return $this->returnLastTwitt();
}

}

$a = new GetLastTwitt('skaizer');
echo $a->getLast();


Download

Question



By the way, the question arose: if we integrate this script into any website, say, into a blog, will this increase the frequency of a search engine robot visiting the site? After all, with each new update, a piece of content on almost all pages of the site will change on Twitter, and the search engine should fix the frequent update of content.

The mirror of this topic in my blog .

Source: https://habr.com/ru/post/52153/


All Articles