jnicol / tweet-php
一个用于查询Twitter API并将推文以HTML列表形式渲染的PHP类
Requires
- php: >=5.3.0
- nojimage/twitter-text-php: dev-master
- themattharris/tmhoauth: dev-master
This package is auto-updated.
Last update: 2024-09-14 00:44:18 UTC
README
一个用于查询Twitter API并将推文以HTML列表形式渲染的PHP类。
特性
- 支持Twitter API v1.1
- 推文被缓存以避免超过Twitter的API请求频率限制
- 如果API请求失败,将提供回退机制
- 配置参数允许您指定要显示的推文数量
- 可以可选地以“Twitter风格”显示日期,例如“12分钟前”
- 您可以自定义包裹推文、推文状态和元信息的HTML
认证
要与Twitter的API交互,您需要在以下位置创建Twitter应用程序:https://dev.twitter.com/apps
创建您的应用程序后,您需要记下以下API值:“Consumer key”、“Consumer secret”、“Access token”、“Access token secret”
用法
您可以将API凭据作为选项传递给类构造函数,以及任何其他配置选项
$TweetPHP = new TweetPHP(array(
'consumer_key' => 'xxxxxxxxxxxxxxxxxxxxx',
'consumer_secret' => 'xxxxxxxxxxxxxxxxxxxxx',
'access_token' => 'xxxxxxxxxxxxxxxxxxxxx',
'access_token_secret' => 'xxxxxxxxxxxxxxxxxxxxx',
'api_params' => array('screen_name' => 'twitteruser')
));
然后您可以这样显示结果
echo $TweetPHP->get_tweet_list();
您还可以检索从Twitter接收到的原始数据
$tweet_array = $TweetPHP->get_tweet_array();
选项
可以通过将键值对数组传递给类构造函数来覆盖选项。至少必须设置上面显示的consumer_key
、consumer_secret
、access_token
、access_token_secret
选项。
您还应该设置一个api_endpoint
和api_params
,这是一个参数数组,将与对Twitter API的调用一起包含。
以下是选项的完整列表及其默认值
'consumer_key' => '',
'consumer_secret' => '',
'access_token' => '',
'access_token_secret' => '',
'api_endpoint' => 'statuses/user_timeline',
'api_params' => array(),
'enable_cache' => true,
'cache_dir' => dirname(__FILE__) . '/cache/', // Where on the server to save cached tweets
'cachetime' => 60 * 60, // Seconds to cache feed (1 hour).
'tweets_to_retrieve' => 25, // Specifies the number of tweets to try and fetch, up to a maximum of 200
'tweets_to_display' => 10, // Number of tweets to display
'twitter_style_dates' => false, // Use twitter style dates e.g. 2 hours ago
'twitter_date_text' => array('seconds', 'minutes', 'about', 'hour', 'ago'),
'date_format' => '%I:%M %p %b %e%O', // The defult date format e.g. 12:08 PM Jun 12th. See: https://php.ac.cn/manual/en/function.strftime.php
'date_lang' => null, // Language for date e.g. 'fr_FR'. See: https://php.ac.cn/manual/en/function.setlocale.php
'twitter_template' => '<h2>Latest tweets</h2><ul id="twitter">{tweets}</ul>',
'tweet_template' => '<li><span class="status">{tweet}</span><span class="meta"><a href="{link}">{date}</a></span></li>',
'error_template' => '<li><span class="status">Our twitter feed is unavailable right now.</span> <span class="meta"><a href="{link}">Follow us on Twitter</a></span></li>',
'nofollow_links' => false, // Add rel="nofollow" attribute to links
'debug' => false
已弃用的选项
以下选项已被弃用。您应该使用api_params
来设置API参数。
'twitter_screen_name' => ''
'ignore_replies' => true
'ignore_retweets' => true
API端点
由于TweetPHP使用Twitter的仅应用程序 API认证模型,它只能访问某些GET端点。
它已与statuses/user_timeline
端点(其默认值)和search/tweets
端点进行了测试。
示例
获取用户的推文时间线
<?php
require_once('TweetPHP.php');
$TweetPHP = new TweetPHP(array(
'consumer_key' => 'xxxxxxxxxxxxxxxxxxxxx',
'consumer_secret' => 'xxxxxxxxxxxxxxxxxxxxx',
'access_token' => 'xxxxxxxxxxxxxxxxxxxxx',
'access_token_secret' => 'xxxxxxxxxxxxxxxxxxxxx',
'api_endpoint' => 'statuses/user_timeline',
'api_params' => array('screen_name' => 'twitteruser')
));
echo $TweetPHP->get_tweet_list();
?>
请注意,在这种情况下可以省略api_endpoint
选项,因为'statuses/user_timeline'是其默认值。
搜索一个标签
<?php
require_once('TweetPHP.php');
$TweetPHP = new TweetPHP(array(
'consumer_key' => 'xxxxxxxxxxxxxxxxxxxxx',
'consumer_secret' => 'xxxxxxxxxxxxxxxxxxxxx',
'access_token' => 'xxxxxxxxxxxxxxxxxxxxx',
'access_token_secret' => 'xxxxxxxxxxxxxxxxxxxxx',
'api_endpoint' => 'search/tweets',
'api_params' => array('q' => '#php', 'result_type'=>'latest')
));
echo $TweetPHP->get_tweet_list();
?>
缓存
使用缓存是因为Twitter限制了每小时可以访问其流量的次数。
当用户时间线首次加载时,生成的HTML列表会被保存为您的Web服务器上的文本文件。此文件的默认位置为:cache/twitter.txt
原始Twitter响应作为序列化数组保存到:cache/twitter-array.txt
您可以使用cache_dir
选项更改这些文件路径。例如,要从您的根公共目录设置路径,请尝试
$_SERVER['DOCUMENT_ROOT'] . '/path/to/my/cache/dir/'
调试
如果您在使用脚本时遇到问题,请将debug
选项设置为true
。这将设置PHP的错误报告级别为E_ALL
,并将显示调试报告。
您还可以在debug
选项设置为false
的情况下,将调试报告作为数组或HTML列表检索
echo $TweetPHP->get_debug_list();
$debug_array = $TweetPHP->get_debug_array();
辅助方法
autolink
将原始推文文本传递给autolink()
,它将所有用户名、标签和URL转换为HTML链接。
$autolinked_tweet = $TweetPHP->autolink($tweet);
如果您想自己处理推文,并使用由get_tweet_array()
返回的数组,这将很有用。
致谢
- 数据源解析使用了Matt Harris的tmhOAuth
- 哈希标签/用户名解析使用了Mike Cochrane的twitter-text-php
- 其他贡献者:Matt Pugh,Dario Bauer,Lee Collings,Dom Abbott