Home Backend Development PHP Tutorial PHP调用Twitter的RSS的实现代码_PHP

PHP调用Twitter的RSS的实现代码_PHP

Jun 01, 2016 pm 12:19 PM
php twitter

杂感
这个栏目最开始调用微博饭否的API来做的,因为众所周知的缘故,饭否无法使用了,后来采用腾讯的滔滔API来实现.2010年1月26日滔滔业务将会开始和QQ空间心情整合,只能考虑放弃。思来想去,最终还是考虑用Twitter来实现,不过Twitter在国内无法访问,不能采用js的方式来调用。本博客的服务器才国外,用php的方式访问Twitter的API应该没有问题,虽然有现成的wordpress插件“Twitter Tools”可以用,但本着尽量少用插件的目的,决定直接用php在wordpress主题里实现。twritter提供的API接口很丰富,研究一下觉得调用Twitter RSS的API比较简单,实现如下功能:

1、抓取twitter RSS的内容,不需要密码,只需要用户名。
2、格式化RSS的内容,显示用户本人的推的内容及时间,排除 @replies 回复给他人的信息内容。

代码如下:
复制代码 代码如下:

$username='xjb';//change this to your twitter username修改为你的twitter 用户名
$feedURL='http://twitter.com/statuses/user_timeline/'.$username.'.rss';
$excludePattern='/'.$username.': @/'; //excludes any @replies排除@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 --转成中国时区
date_default_timezone_set('Asia/Shanghai');
$i++;

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

  • (pubDate)); ?>)





  • 源代码
    复制代码 代码如下:


    $username='xjb'; //change this to your twitter username --修改为你的twitter 用户名
    $feedURL='http://twitter.com/statuses/user_timeline/'.$username.'.rss';
    $excludePattern='/'.$username.': @/'; //excludes any @replies --排除 @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 --转成中国时区
    $i++;

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

  • (pubDate)); ?>)




  • Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1665
    14
    PHP Tutorial
    1269
    29
    C# Tutorial
    1249
    24
    The top ten free platform recommendations for real-time data on currency circle markets are released The top ten free platform recommendations for real-time data on currency circle markets are released Apr 22, 2025 am 08:12 AM

    Cryptocurrency data platforms suitable for beginners include CoinMarketCap and non-small trumpet. 1. CoinMarketCap provides global real-time price, market value, and trading volume rankings for novice and basic analysis needs. 2. The non-small quotation provides a Chinese-friendly interface, suitable for Chinese users to quickly screen low-risk potential projects.

    The latest news APP ranking recommendation in the currency circle (authoritative release in 2025) The latest news APP ranking recommendation in the currency circle (authoritative release in 2025) Apr 21, 2025 pm 09:33 PM

    The best cryptocurrency trading and analysis platforms include: 1. OKX: the world's number one in trading volume, supports multiple transactions, provides AI market analysis and on-chain data monitoring. 2. Binance: The world's largest exchange, providing in-depth market conditions and new currency first-time offerings. 3. Sesame Open Door: Known for spot trading and OTC channels, it provides automated trading strategies. 4. CoinMarketCap: an authoritative market data platform, covering 20,000 currencies. 5. CoinGecko: Known for community sentiment analysis, it provides DeFi and NFT trend monitoring. 6. Non-small account: a domestic market platform, providing analysis of linkage between A-shares and currency markets. 7. On-chain Finance: Focus on blockchain news and update in-depth reports every day. 8. Golden Finance: 24 small

    The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

    IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

    A summary of the top ten virtual currency trading platforms A summary of the top ten virtual currency trading platforms Apr 21, 2025 am 09:33 AM

    This article summarizes the information of the top ten virtual currency trading platform APPs, emphasizes the importance of secure access to the platform through official channels, and provides verification methods. At the same time, the article reminds investors to consider factors such as security, transaction fees, currency selection when choosing an APP, and pay attention to the risks of virtual currency trading.

    What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

    Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

    Binance Trading App Official Website Download Portal Binance Trading App Official Website Binance Trading App Official Website Download Portal Binance Trading App Official Website Apr 24, 2025 pm 02:18 PM

    To safely access the Binance official platform and download the APP, you can use the following steps: 1) Use a trusted search engine to search for "Binance" and check the domain name; 2) View the official social media to obtain the URL; 3) Consult the official customer service to confirm the URL; 4) Use a trusted navigation website. The steps to download the APP include: 1) Visit Binance official website; 2) Find the APP download portal; 3) Select the download method (scan the QR code, download the app store, and directly download the APK file).

    Magacoin Finance becomes the new favorite of investors Magacoin Finance becomes the new favorite of investors Apr 20, 2025 pm 08:12 PM

    Bitcoin approaches the $80,000 mark, leading a new round of rise in the crypto market. Bitcoin (BTC) continues to rise, aiming at the key price of $80,000. However, keen investors are turning their attention to another dark horse, MagacoinFinance, and analysts predict that it will soon break through the $1 mark. Bitcoin: Institutions favor global applications to drive price increases. Bitcoin is a leader in the crypto market, and has risen strongly recently, approaching the psychological price of $80,000. Market analysts believe that the return of institutional investors, clear supervision of major markets and continued growth in global applications are the main factors driving this rise. However, can Bitcoin continue to rise at this price level or will it usher in a pullback? This uncertainty prompts investors to seek diversified investment strategies

    Binance download link Binance download path Binance download link Binance download path Apr 24, 2025 pm 02:12 PM

    To safely download the Binance APP, you need to go through the official channels: 1. Visit the Binance official website, 2. Find and click the APP download portal, 3. Choose to scan the QR code, app store, or directly download the APK file to download to ensure that the link and developer information are authentic, and enable two-factor verification to protect the security of the account.

    See all articles