使用PHP显示最近的Twitter tweets
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PHP显示最近的Twitter tweets相关的知识,希望对你有一定的参考价值。
If you’ve ever wanted to display your latest Twitter tweets on a website, here is method to do that using php.
<?php /** * TWITTER FEED PARSER * * @version 1.1.1 * @author Jonathan Nicol * @link http://f6design.com/journal/2010/10/07/display-recent-twitter-tweets-using-php/ * * Notes: * We employ caching because Twitter only allows their RSS feeds to be accesssed 150 * times an hour per user client. * -- * Dates can be displayed in Twitter style (e.g. "1 hour ago") by setting the * $twitter_style_dates param to true. * * Credits: * Hashtag/username parsing based on: http://snipplr.com/view/16221/get-twitter-tweets/ * Feed caching: http://www.addedbytes.com/articles/caching-output-in-php/ * Feed parsing: http://boagworld.com/forum/comments.php?DiscussionID=4639 */ function display_latest_tweets( $twitter_user_id, $cache_file = './twitter.txt', $tweets_to_display = 100, $ignore_replies = false, $twitter_wrap_open = '<h2>Latest tweets</h2><ul id="twitter">', $twitter_wrap_close = '</ul>', $tweet_wrap_open = '<li><span class="status">', $meta_wrap_open = '</span><span class="meta"> ', $meta_wrap_close = '</span>', $tweet_wrap_close = '</li>', $date_format = 'g:i A M jS', $twitter_style_dates = false){ // Seconds to cache feed (1 hour). $cachetime = 60*60; // Time that the cache was last filled. // A flag so we know if the feed was successfully parsed. $tweet_found = false; // Show file from cache if still valid. $tweet_found = true; // Display tweets from the cache. } else { // Cache file not found, or old. Fetch the RSS feed from Twitter. if($rss) { // Parse the RSS feed to an XML object. if($xml !== false) { // Error check: Make sure there is at least one item. $tweet_count = 0; // Start output buffering. // Open the twitter wrapping element. // Iterate over tweets. foreach($xml->channel->item as $tweet) { // Twitter feeds begin with the username, "e.g. User name: Blah" // so we need to strip that from the front of our tweet. // If we are not gnoring replies, or tweet is not a reply, process it. if ($tweet_first_char!='@' || $ignore_replies==false){ $tweet_found = true; $tweet_count++; // Add hyperlink html tags to any urls, twitter ids or hashtags in the tweet. $tweet_desc = preg_replace('/(^|[ s])@([^s" <:]*)/is', '$1<a href="http://twitter.com/$2">@$2</a>', $tweet_desc); $tweet_desc = preg_replace('/(^|[ s])#([^s" <:]*)/is', '$1<a href="http://twitter.com/search?q=%23$2">#$2</a>', $tweet_desc); // Convert Tweet display time to a UNIX timestamp. Twitter timestamps are in UTC/GMT time. if ($twitter_style_dates){ // Current UNIX timestamp. switch ($time_diff) { case ($time_diff < 60): $display_time = $time_diff.' seconds ago'; break; case ($time_diff >= 60 && $time_diff < 3600): $display_time = $min.' minutes ago'; break; case ($time_diff >= 3600 && $time_diff < 86400): $display_time = 'about '.$hour.' hour'; if ($hour > 1){ $display_time .= 's'; } $display_time .= ' ago'; break; default: break; } } else { } // Render the tweet. $twitter_html .= $tweet_wrap_open.$tweet_desc.$meta_wrap_open.'<a href="http://twitter.com/'.$twitter_user_id.'">'.$display_time.'</a>'.$meta_wrap_close.$tweet_wrap_close; } // If we have processed enough tweets, stop. if ($tweet_count >= $tweets_to_display){ break; } } // Close the twitter wrapping element. $twitter_html .= $twitter_wrap_close; echo $twitter_html; // Generate a new cache file. // Save the contents of output buffer to the file, and flush the buffer. } } } } // In case the RSS feed did not parse or load correctly, show a link to the Twitter account. if (!$tweet_found){ echo $twitter_wrap_open.$tweet_wrap_open.'Oops, our twitter feed is unavailable right now. '.$meta_wrap_open.'<a href="http://twitter.com/'.$twitter_user_id.'">Follow us on Twitter</a>'.$meta_wrap_close.$tweet_wrap_close.$twitter_wrap_close; } } display_latest_tweets('YOUR_TWITTER_ID'); ?>
以上是关于使用PHP显示最近的Twitter tweets的主要内容,如果未能解决你的问题,请参考以下文章
Twitter-打印用户的最新tweet(摘自PHP中的RSS提要和linkifies链接)
如何使用 PHP 在 JSON Twitter V2 响应中获取用户名?
Twitter RSS提要解析器,用于在网站上显示tweet(包括缓存)