When developing a site, you may need to get some of the latest posts (tweets) from Twitter. This can be either a personal tape of a regular user or the official tape of a company.
There are at least 2 ways to solve this problem.
Method 1:
UPD: This method no longer works.
The first way is to get the RSS file with tweets from the tape.
The first difficulty that can arise not only in this method is that work is possible only with
user_id , and not just a login.
')
User information by login can be easily obtained via XML.
$username = 'newpdv'; $user_info = simplexml_load_file('http://twitter.com/users/show.xml?screen_name='.$username); $user_id = $user_info[0]->id;
The resulting XML file stores all user information.
Knowing
user_id without much difficulty, you can get posts from the tape.
$timeline = simplexml_load_file('http://twitter.com/statuses/user_timeline/'.$user_id.'.rss');
And display the latest posts.
$count = 3; for($i=0; $i<$count; $i++){ echo '<b>'.date('dmY G:i',strtotime($timeline->channel->item[$i]->pubDate)).'</b></br>'; echo $timeline->channel->item[$i]->title.'</br>'; echo '<a href="'.$timeline->channel->item[$i]->link.'">Twitter</a></br></br>'; }
Method 2:
The second way is to use the Twitter API. This method is more complex and requires registration, but using the API offers much more features.
The first thing you need is to register the application Twittet on the page
dev.twitter.com/apps/newWe are required to enter the application name, description, the site where the application will be applied, the type (in our case, Browser), the access mode (in our case, Read-only is enough) and, if necessary, the icon.
After registration, we will be given the keys Consumer key and Consumer secret.
To facilitate our work, we will use one of the ready-made libraries offered by Twitter called TwitterOAuth, written by Abraham Williams.
Other libraries, including for other programming languages,
visit dev.twitter.com/pages/librariesDownload the TwitterOAuth library in the
config.php file and specify the applications CONSUMER_KEY, CONSUMER_SECRET and OAUTH_CALLBACK obtained after registration.
Having run the library for the first time, we will need to allow it to access Twitter by clicking on
Sign in with Twitter , and on the Twitter page answering the question whether the application can access your page - Allow.
Now let's edit index.php a bit for our task.
[...] $content = $connection->get('account/verify_credentials'); $count = 3; $timeline = $connection->get('statuses/user_timeline', array('count' => $count, 'include_rts' => 1)); foreach ($timeline as $twitt) { echo '<b>'.date('dmY G:i',strtotime($twitt->created_at)).'</b></br>'; echo $twitt->text.'</br>'; }
And we get what we need.
In addition
As noted
dharrya , the use of any of the methods in its pure form can be irrational. You should use Twitter caching in any way possible, be it a database or file system.
Conclusion
In my first article on Habré, there were two ways to get tweets from the tape.
In terms of execution, both options are about the same (can be seen on screen).
Choose you. If you just need to get a list of the latest tweets, then the first method is much shorter and does not require any special actions, both from the developer and from the user. If your requirements are not limited to these, then the huge API capabilities are what you need.