PHP calls Twitter's RSS implementation code

  • 2020-03-31 20:30:11
  • OfStack

< img Alt = style of border = 0 SRC = "http://www.watch-life.net/images/fanfou.jpg" border = 0 >
At the beginning of this column, I called fanfou's API of weibo, but it could not be used because of the well-known reason, and later, I adopted tencent's surging API to realize it. On January 26, 2010, surging business will start to integrate with QQ space, so I can only consider giving up. However, Twitter is not accessible in China and cannot be invoked in js mode. This blog's server is only abroad, with PHP access to Twitter API should be no problem, although there is an existing wordpress plug-in "Twitter Tools" can be used, but in the purpose of using as little as possible plug-in, decided to use PHP directly in the wordpress theme. The API interface provided by twritter is very rich. I think it is relatively simple to call the API of Twitter RSS to achieve the following functions:

1. You don't need a password to grab the content of twitter RSS, just a user name.
2. Format the content of RSS, display the content and time of the user's tweets, and exclude the information content of @replies replied to others.

The code is as follows:

<!-- my tritter --> 
<?php 
$username='xjb';//Change this to your twitter username to change this to your twitter username
$feedURL='http://twitter.com/statuses/user_timeline/'.$username.'.rss'; 
$excludePattern='/'.$username.': @/'; //Excludes any @ the replies to rule out @ the replies
$count=5;// show count 
$i=0; 

if(!$xml=simplexml_load_file($feedURL)){ 
trigger_error('Error',E_USER_ERROR); 
} 
foreach($xml->channel->item as $item) { 
if ( ! preg_match("$excludePattern", $item->title)) { 
$filteredTitle=htmlspecialchars("$item->title"); 
$filteredTitle=str_replace("$username: ","",$filteredTitle); 
//Convert the time zone in China -- Convert the time zone to China
date_default_timezone_set('Asia/Shanghai'); 
$i++; 

if($i>$count) 
{ 
break; 
} 
?> 
<li><?php echo $filteredTitle; ?> 
(<?php echo date("Y-m-d H:i:s",strtotime($item->pubDate)); ?>)</li> 
<?php } } ?> 
<div align="right"> 
<a href="http://twitter.com/xjb" target="_blank"> More and more ...</a></div> 
<!-- my tritter -->

The source code

<!-- my tritter --> 
<?php 

$username='xjb'; //Change this to your twitter username - change this to your twitter username
$feedURL='http://twitter.com/statuses/user_timeline/'.$username.'.rss'; 
$excludePattern='/'.$username.': @/'; //Excludes any @ the replies - ruled out @ the replies
$count=5;// show count 
$i=0; 

if(!$xml=simplexml_load_file($feedURL)){ 
trigger_error('Error',E_USER_ERROR); 
} 
foreach($xml->channel->item as $item) { 
if ( ! preg_match("$excludePattern", $item->title)) { 
$filteredTitle=htmlspecialchars("$item->title"); 
$filteredTitle=str_replace("$username: ","",$filteredTitle); 
date_default_timezone_set('Asia/Shanghai'); //Convert the time zone in China -- Convert the time zone to China
$i++; 

if($i>$count) 
{ 
break; 
} 
?> 

<li><?php echo $filteredTitle; ?>(<?php echo date("Y-m-d H:i:s",strtotime($item->pubDate)); ?>)</li> 
<?php } } ?> 
<div align="right"><a href="http://twitter.com/xjb" target="_blank"> More and more ...</a></div> 
<!-- my tritter -->

Related articles: