[WordPress] 外掛分享: Get Tweets in PHP

首頁外掛目錄 › Get Tweets in PHP
WordPress 外掛 Get Tweets in PHP 的封面圖片
60+
安裝啟用
★★★★★
5/5 分(2 則評價)
3367 天前
最後更新
問題解決
WordPress 4.0+ v1.2 上架:2015-08-23

內容簡介

這個外掛程式會新增 PHP 類別 GetTweetsInPhp,您可以如下描述使用這個類別從 Twitter 帳戶檢索最新推文 (tweet),隨後能針對您 PHP 程式碼做出不同的方式處理這些推文。

備註:在使用這個外掛程式之前,您應先建立 Twitter 應用程式來源,請參考 http://apps.twitter.com。

功能

從 Twitter 帳戶取得最新 N 則推文。
以 HTML 格式化的方式取得推文內容(包含每個實體的連結)。
支援快取。
設計給開發人員使用。
非常輕量且簡單。
適用於 v1.1 Twitter API。
安裝和解除安裝容易(只需新增/移除外掛程式檔案)。
不會永久地在您的資料庫儲存任何資料(只會暫時快取資料,如果快取被啟用)。
由 Netgloo 團隊自豪製作。

範例使用

從 @netglooweb 取得最新推文的範例程式碼如下:

// 設定組態
$configs = [
// 設定您的 Twitter 應用程式的權杖
'consumer_key' => 'CONSUMER_KEY',
'consumer_secret' => 'CONSUMER_SECRET',

// Twitter 帳戶名稱
'screen_name' => 'netglooweb',

// 推文數量
'count' => 5,
];

// 使用函式 get_tweets 取得最新推文
$tweets = \Netgloo\GetTweetsInPhp::get_tweets($configs);

// ...

// 對於每則推文,顯示 HTML 文字和附加的圖片
foreach ($tweets as $tweet) {

echo "<p>";
echo $tweet->n_html_text;

if ($tweet->n_has_media_photo) {
echo "<img src='{$tweet->n_media_photo_url}' width='100%' />";
}

echo "</p>";

}

// ...

這樣就完成了!祝您玩得愉快!

組態說明

函式 get_tweets() 接受一個組態設定的陣列:

$configs = [

// --- 必填項目 ---

// 您的 Twitter 應用程式權杖
'consumer_key' => '...',
'consumer_secret' => '...',

// Twitter 帳戶名稱
'screen_name' => '...',

// --- 可選項目 ---

// 推文數量
'count' => 20,

// 是否包含轉推 (retweet)
'include_rts' => true,

// 如果是轉推,是否顯示 "Retweeted by ..." 字樣
'show_retweeted_by' => true,

// 是否啟用快取
// 建議在啟用網站時啟用快取,以避免達到 Twitter 的 API 速率限制 (300 次請求 / 15 分鐘)
'cache_enabled' => false,

// 快取過期時間 (以秒為單位)
// 增加值以優化網站速度,減少值以獲得更實時的行為 (但不能少於 4 秒,以免達到速率限制)
'cache_expiration' => 60,

// 範本

// 轉推文字範本
'retweeted_by_template' =>
'<em> Retweeted by {{user_name}}</em>',

// 哈希標籤連結範本
'hashtag_link_template' =>
'<a href="{{hashtag_link}}" rel="nofollow" target="_blank">' .
'#{{hashtag_text}}</a>',

// URL 連結範本
'url_link_template' =>
'<a href="{{url_link}}" rel="nofollow" target="_blank" ' .
'title="{{url_title}}"&

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.2) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Get Tweets in PHP」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

This plugin will add the PHP class GetTweetsInPhp. You can use this class as described below for retrieving latest tweets from a Twitter account, then handle the tweets as you want in your PHP code.
Note: you should create a Twitter app before using this plugin. You can do it from here: http://apps.twitter.com.
Features

Get latest N tweets from a Twitter account.
Get the tweet’s text formatted as HTML (with links for each entities).
Cache support.
Made for developers.
Really light and simple.
Works with the v1.1 Twitter API.
Trivial install/uninstall (only add/remove the plugin’s files).
No any data will be permanently stored in your database (only transient data
if the cache is enabled).
Proudly coded by Netgloo.

Example usage
Get and show latest tweets from @netglooweb:
// Set configurations
$configs = [
// Set here tokens from your Twitter's app
'consumer_key' => 'CONSUMER_KEY',
'consumer_secret' => 'CONSUMER_SECRET',

// The Twitter account name
'screen_name' => 'netglooweb',

// The number of tweets
'count' => 5,
];

// Get latest tweets using the function get_tweets
$tweets = \Netgloo\GetTweetsInPhp::get_tweets($configs);

// ...

// For each tweet show the HTML text and the attached image
foreach ($tweets as $tweet) {

echo "

";
echo $tweet->n_html_text;

if ($tweet->n_has_media_photo) {
echo "";
}

echo "

";

}

// ...

That’s all! Have fun!
Configurations
The get_tweets() function takes an array of configurations:
$configs = [

// --- Required ---

// The tokens from your Twitter's app
'consumer_key' => '...',
'consumer_secret' => '...',

// The Twitter account name
'screen_name' => '...',

// --- Optional ---

// The number of tweets
'count' => 20,

// Include also the retweets
'include_rts' => true,

// In the HTML text will be showed "Retweeted by ..." if the tweet
// is a retweet
'show_retweeted_by' => true,

// Enable the cache
// It is recommended to activate the cache, when you put live
// your website, in order to avoid to reach the Twitter's api rate
// limit of 300 requests / 15-min.
'cache_enabled' => false,

// Cache expiration (in seconds)
// Increase the value to optimize the website's speed, decrease
// the value if you want a more real-time behaviour (but not
// less than 4 seconds to avoid to reach the rate limit).
'cache_expiration' => 60,

// Templates

// Retweeted by text template
'retweeted_by_template' =>
' Retweeted by {{user_name}}',

// Hash tag link template
'hashtag_link_template' =>
'' .
'#{{hashtag_text}}
',

// Url link template
'url_link_template' =>
'{{url_text}}',

// User mention link template
'user_mention_link_template' =>
'@{{user_mention_text}}',

// Media link template
'media_link_template' =>
'{{media_text}}'

];

$tweets = \Netgloo\GetTweetsInPhp::get_tweets($configs);

Returned values
The get_tweets() function will return an Array of tweets. On each tweet object are available these properties:

n_html_text (String) The tweet text formatted as HTML, with links on each entities.
n_is_retweeted (Boolean) True if the curret tweet is a retweet.
n_has_media_photo (Boolean) True if the current tweet has an attached photo.
n_media_photo_url (String) The url of the tweet’s attached photo.
n_media_photo_urls (Array) If the tweet has more than one attached photos this properties contains all the urls.

Other available properties are those returned from the user_timeline Twitter’s API.
These are some useful ones:

created_at
retweet_count
user->name
user->screen_name
user->profile_image_url

If the properties n_is_retweeted is true the current tweet is a “re-tweet” and the retweeted_status object is available:

retweeted_status->user->name
retweeted_status->user->screen_name
retweeted_status->retweet_count

Example
This code use some of the above properties:
// ...

$tweets = \Netgloo\GetTweetsInPhp::get_tweets($configs);

foreach ($tweets as $tweet) {
echo $tweet->created_at . "
";
echo $tweet->n_html_text . "
";
if ($tweet->n_has_media_photo) {
echo $tweet->n_media_photo_url . "
";
}
}

// ...

Limitations
Since we rely on the Twitter’s user_timeline API, you should read the following docs for taking in account any API’s limitation:

https://dev.twitter.com/rest/reference/get/statuses/user_timeline
https://dev.twitter.com/rest/public/timelines

Contributing
For patches, bug reports, suggestions, requests for features there is a Git repository on GitHub here:
https://github.com/netgloo/get-tweets-in-php

延伸相關外掛

文章
Filter
Apply Filters
Mastodon