Home >Backend Development >PHP Tutorial >PHP calls Twitter's RSS implementation code_PHP tutorial

PHP calls Twitter's RSS implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:40:44900browse

杂感
This column was first implemented by calling Weibo Fanfou’s API. Due to well-known reasons, Fanfou cannot be used. Later, Tencent’s Taotao API was used to implement it. On January 26, 2010, Taotao business will It will start to integrate with the QQ space mood, and I 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:

Copy code The code is as follows:



Source code
Copy code The code is as follows:



$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 @replies --exclude @replies content
$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 to China time zone
$i++;

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

  • (pubDate)); ?>)




  • www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321338.htmlTechArticleThis column was originally created by calling Weibo Fanfou’s API. Because of well-known reasons, Fanfou cannot be used. Later, Tencent's Taotao API was used to implement it. On January 26, 2010, Taotao business will...