Title: Get Tweets in PHP
Author: azanelli
Published: <strong>2015 年 8 月 24 日</strong>
Last modified: 2016 年 12 月 28 日

---

搜索插件

![](https://ps.w.org/get-tweets-in-php/assets/banner-772x250.png?rev=1240948)

**该插件尚未通过WordPress的最新3个主要版本进行测试**。 当与较新版本的WordPress一起
使用时，可能不再受到维护或支持，并且可能会存在兼容性问题。

![](https://ps.w.org/get-tweets-in-php/assets/icon.svg?rev=1240948)

# Get Tweets in PHP

 作者：[azanelli](https://profiles.wordpress.org/azanelli/)

[下载](https://downloads.wordpress.org/plugin/get-tweets-in-php.1.2.zip)

 * [详情](https://cn.wordpress.org/plugins/get-tweets-in-php/#description)
 * [评价](https://cn.wordpress.org/plugins/get-tweets-in-php/#reviews)
 *  [安装](https://cn.wordpress.org/plugins/get-tweets-in-php/#installation)
 * [开发进展](https://cn.wordpress.org/plugins/get-tweets-in-php/#developers)

 [支持](https://wordpress.org/support/plugin/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](http://apps.twitter.com).

#### 特点

 * 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](http://netgloo.com/en).

#### Example usage

Get and show latest tweets from [@netglooweb](http://twitter.com/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 "<p>";
      echo $tweet->n_html_text;

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

      echo "</p>";

    }

    // ...
    ```

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' => 
        '<em> Retweeted by {{user_name}}</em>',

      // Hash tag link template
      'hashtag_link_template' => 
        '<a href="{{hashtag_link}}" rel="nofollow" target="_blank">' .
        '#{{hashtag_text}}</a>',

      // Url link template
      'url_link_template' => 
        '<a href="{{url_link}}" rel="nofollow" target="_blank" ' .
        'title="{{url_title}}">{{url_text}}</a>',

      // User mention link template
      'user_mention_link_template' => 
        '<a href="{{user_mention_link}}" rel="nofollow" target="_blank" ' .
        'title="{{user_mention_title}}">@{{user_mention_text}}</a>',

      // Media link template
      'media_link_template' => 
        '<a href="{{media_link}}" rel="nofollow" target="_blank" ' .
        'title="{{media_title}}">{{media_text}}</a>'

    ];

    $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](https://dev.twitter.com/rest/reference/get/statuses/user_timeline).

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 . "<br/>";
      echo $tweet->n_html_text . "<br/>";
      if ($tweet->n_has_media_photo) {
        echo $tweet->n_media_photo_url  . "<br/>";
      }
    }

    // ...
    ```

#### 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

## 安装

#### 需求

To work this plugin, following component need to be installed in your server.

 * PHP version 5.3 or higher
 * cURL
 * WordPress 4.2.2 or higher

#### 安装

Put the plugin to your WordPress’ plugins folder and activate it from the Admin 
Backend.

#### 移除设定

Just delete the plugin from WordPress.

## 常见问题

  Why the “n” before your custom tweet’s properties?

Our custom properties (i.e. properties not from the Twitter’s API) are prefixed 
with an “n”. The “n” is the first character in “Netgloo” ;).

## 评价

![](https://secure.gravatar.com/avatar/15829714ade181f443b5babbcfd2f8634d06590561563a123f796b592ab3d0fc?
s=60&d=retro&r=g)

### 󠀁[Awesome!](https://wordpress.org/support/topic/awesome-4691/)󠁿

 [gmanfredi](https://profiles.wordpress.org/gmanfredi/) 2017 年 6 月 7 日

Awesome!

 [ 阅读所有2条评价 ](https://wordpress.org/support/plugin/get-tweets-in-php/reviews/)

## 贡献者及开发者

「Get Tweets in PHP」是开源软件。 以下人员对此插件做出了贡献。

贡献者

 *   [ azanelli ](https://profiles.wordpress.org/azanelli/)
 *   [ Netgloo ](https://profiles.wordpress.org/netgloo/)
 *   [ aboutnick ](https://profiles.wordpress.org/aboutnick/)

[帮助将「Get Tweets in PHP」翻译成简体中文。](https://translate.wordpress.org/projects/wp-plugins/get-tweets-in-php)

### 对开发感兴趣吗?

您可以[浏览代码](https://plugins.trac.wordpress.org/browser/get-tweets-in-php/)，
查看[SVN仓库](https://plugins.svn.wordpress.org/get-tweets-in-php/)，或通过[RSS](https://plugins.trac.wordpress.org/log/get-tweets-in-php/?limit=100&mode=stop_on_copy&format=rss)
订阅[开发日志](https://plugins.trac.wordpress.org/log/get-tweets-in-php/)。

## 更新日志

#### 1.2

 * Custom HTML templates for links and “retweeted by” text.
 * New hashtag link: https://twitter.com/hashtag.
 * Get media urls with https.
 * Cache disabled by default (as in the documentation).

#### 1.1

 * Bug fix setting transient cache name.

#### 1.0

 * First release.

## 额外信息

 *  版本 **1.2**
 *  最后更新：**9 年前**
 *  活跃安装数量 **50+**
 *  WordPress 版本 ** 4.0 或更高版本 **
 *  已测试的最高版本为 **4.7.33**
 *  语言
 * [English (US)](https://wordpress.org/plugins/get-tweets-in-php/)
 * 标签
 * [developers](https://cn.wordpress.org/plugins/tags/developers/)[latest tweets](https://cn.wordpress.org/plugins/tags/latest-tweets/)
   [tweet](https://cn.wordpress.org/plugins/tags/tweet/)[tweets](https://cn.wordpress.org/plugins/tags/tweets/)
   [twitter](https://cn.wordpress.org/plugins/tags/twitter/)
 *  [高级视图](https://cn.wordpress.org/plugins/get-tweets-in-php/advanced/)

## 评级

 5 星（最高 5 星）。

 *  [  2 条 5 星评价     ](https://wordpress.org/support/plugin/get-tweets-in-php/reviews/?filter=5)
 *  [  0 条 4 星评价     ](https://wordpress.org/support/plugin/get-tweets-in-php/reviews/?filter=4)
 *  [  0 条 3 星评价     ](https://wordpress.org/support/plugin/get-tweets-in-php/reviews/?filter=3)
 *  [  0 条 2 星评价     ](https://wordpress.org/support/plugin/get-tweets-in-php/reviews/?filter=2)
 *  [  0 条 1 星评价     ](https://wordpress.org/support/plugin/get-tweets-in-php/reviews/?filter=1)

[Your review](https://wordpress.org/support/plugin/get-tweets-in-php/reviews/#new-post)

[查看全部评论](https://wordpress.org/support/plugin/get-tweets-in-php/reviews/)

## 贡献者

 *   [ azanelli ](https://profiles.wordpress.org/azanelli/)
 *   [ Netgloo ](https://profiles.wordpress.org/netgloo/)
 *   [ aboutnick ](https://profiles.wordpress.org/aboutnick/)

## 支持

有话要说吗？是否需要帮助？

 [查看支持论坛](https://wordpress.org/support/plugin/get-tweets-in-php/)