Home > Article > Backend Development > Example code: Using PHP to call Twitter's RSS_PHP tutorial
The right sidebar of the "Shouwangxuan" blog originally had a "Miscellaneous Feelings" column, which was used to record short sentences that could not be written in large lengths, or short sentences and remarks that I liked more.
This column was first implemented by calling Fanfou’s API on Weibo. As everyone knows, Fanfou cannot be used. Later, Tencent’s Taotao API was used to implement it. 2010 1 On May 26th, Taotao business will begin to be integrated with QQ Space Mood, so we can only consider giving up. After much thought, I finally considered using Twitter to implement it. However, Twitter is inaccessible in China and cannot be called using js. The server of this blog is overseas. There should be no problem in using PHP to access the Twitter API. Although there is a ready-made WordPress plug-in "Twitter Tools" available, in order to use as few plug-ins as possible, I decided to use PHP directly in the WordPress theme. accomplish. The API interfaces provided by twitter are very rich. After some research, I found that calling the Twitter RSS API is relatively simple and can implement the following functions:
1. Capture the content of twitter RSS. No password is required, only a username.
2. Format the RSS content, display the content and time of the user's own tweets, and exclude the content of @replies' replies to others.
The code is as follows:
01:
02: 03: $username='xjb';/ /change this to your twitter username
04: $feedURL='http://twitter.com/statuses/user_timeline/'.$username.'.rss';
05: $excludePattern='/'.$username.': @/'; //excludes any @repliesexclude @replies content
06: $count=5;// show count
07: $i=0;
08:
09: if(!$xml=simplexml_load_file($feedURL)){
10: trigger_error('Error',E_USER_ERROR);
11: }
12: foreach( $xml->channel->item as $item) {
13: if ( ! preg_match("$excludePattern", $item->title)) {
14: $filteredTitle=htmlspecialchars(" $item->title");
15: $filteredTitle=str_replace("$username: ","",$filteredTitle);
16: //Convert the time zone in China --convert to China Time zone
17: date_default_timezone_set('Asia/Shanghai');
18: $i++;
19:
20: if($i>$count)
21: {
22: break;
23: }
24: ?>
25:
Source code download: twitter-rss.rar