PHP invoke Twitter's RSS implementation code _php Tips

Source: Internet
Author: User
Tags echo date

This column began to call the micro-Bo Rice no API to do, because the well-known reason, the rice can not be used, and then use Tencent's surging API to achieve. January 26, 2010 surging business will start and QQ space mood integration, can only consider giving up. Reasoning, eventually consider using Twitter to achieve, but Twitter is not accessible in the country, can not use JS way to invoke. This blog server only abroad, the way to use PHP to access Twitter API should be no problem, although there are ready-made WordPress plug-ins "Twitter Tools" can be used, but in the spirit of minimizing the purpose of using Plug-ins, decided to use PHP directly in the theme of WordPress. Twritter provides a rich API interface, and studies the API that calls Twitter RSS is relatively simple, to achieve the following functions:

1, grab the content of Twitter RSS, do not need a password, only need user name.
2, format the content of RSS, display the user's push content and time, excluding @replies reply to others information content.

The code is as follows:
Copy Code code as follows:

<!--my Tritter-->
<?php
$username = ' XJB ';//change this to your Twitter username modified for your Twitter username
$feedURL = ' http://twitter.com/statuses/user_timeline/'. $username. RSS ';
$excludePattern = '/'. $username. ': @/'; Excludes any @replies excluding @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);
Convert the time zone into 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 ...</a></div>
<!--my Tritter-->

Source
Copy Code code as follows:

<!--my Tritter-->
<?php

$username = ' XJB '; Change this to your Twitter username--Modify your Twitter username
$feedURL = ' http://twitter.com/statuses/user_timeline/'. $username. RSS ';
$excludePattern = '/'. $username. ': @/'; Excludes any @replies--excluding @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 into 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 ...</a></div>
<!--my Tritter-->

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.