mutantlabs/twitter-rest

一个简单的类,用于通过 OAuth 与 Twitter API v1.1 接口,并使用 GET 方法返回 JSON 格式的推文。使用 RestService 和 twitteroauth。

dev-master 2013-06-28 15:11 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:13:07 UTC


README

一个简单的类,用于通过 OAuth 与 Twitter API v1.1 接口,并使用 GET 方法返回 JSON 格式的推文。使用 RestService 和 twitteroauth。

#安装

需要 https://github.com/marcj/php-rest-service

使用 Composer 安装 twitter-rest

创建一个 composer.json

{
    "require": {
        "mutantlabs/twitter-rest": "dev-master"
    }
}

然后运行

$ wget https://getcomposer.org.cn/composer.phar
$ php composer.phar install

需求

  • PHP 5.3 及以上。
  • PHPUnit 以执行测试套件。
  • 在 mod_rewrite (.htaccess) 或其他 Web 服务器配置中设置 PATH_INFO

示例

//apache .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

#配置

创建 config.php 如下

设置您的 config.php(使用来自 https://github.com/abraham/twitteroauth 的 twitteroauth)

define('CONSUMER_KEY', 'CONSUMER_KEY_HERE');
define('CONSUMER_SECRET', 'CONSUMER_SECRET_HERE');
define('OAUTH_TOKEN', 'OAUTH_TOKEN_HERE');
define('OAUTH_TOKEN_SECRET', 'OAUTH_TOKEN_SECRET_HERE');
define('OAUTH_CALLBACK', 'http://example.com/twitteroauth/callback.php');

#TwitterRestAPI 的示例使用

包含 vendor/autoload.php 以在您的脚本中使类可用。

include 'vendor/autoload.php';

##基本使用

构造新的 TwitterRestAPI

use TwitterRest\TwitterRestAPI;
require_once('config.php');
$twitterRestApi = new TwitterRestAPI();

获取一些推文

$tweets = $twitterRestApi->getCachedUserStatus();
$arrTweets = array();
foreach($tweets as $key => $status) {
    $arrTweets[$key] = array(
        'created_at' => $status->created_at,
        'text' => $status->text
    );
}
echo json_encode($arrTweets);

注意 - 要使用 getCachedUserStatus() - 您需要使您的 Apache 服务器成为 twitter_result.data 的所有者

sudo chown -R www-data:www-data twitter_result.data

#使用 php-rest-service 创建 REST API 方法

use TwitterRest\TwitterRestAPI;
require_once('config.php');

TwitterRestAPI::create('/')
    ->addGetRoute('', function(){
        $twitterRestApi = new TwitterRestAPI();
        $tweets = $twitterRestApi->getCachedUserStatus();
        $arrTweets = array();
        foreach($tweets as $key => $status) {
            $arrTweets[$key] = array(
                'created_at' => $status->created_at,
                'text' => $status->text
            );
        }
        return $arrTweets;
    })
    ->run();

扩展认证流程来自 https://github.com/abraham/twitteroauth

    ->addGetRoute('authenticate', function(){
            //When a user lands on /authenticate we build a new TwitterOAuth object using the client credentials.
            $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

            //Using the built $connection object you will ask Twitter for temporary credentials. The oauth_callback value is required.
            $temporary_credentials = $connection->getRequestToken(OAUTH_CALLBACK);

            //Once we have temporary credentials the user has to go to Twitter and authorize the app to access and updates their data.
            $redirect_url = $connection->getAuthorizeURL($temporary_credentials, FALSE);

            return array($temporary_credentials,$redirect_url);
        })

用户现在在 twitter.com,可能需要登录。一旦通过 Twitter 认证,他们可能需要点击允许/拒绝,或者将自动重定向回回调。在这个例子中是 example.domain.com/success

一旦用户返回到 /success 并允许访问,我们需要使用临时凭证构建一个新的 TwitterOAuth 对象。

        ->addGetRoute('success', function(){
            $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_GET['oauth_token'],
                $_GET['oauth_verifier']);

            //Now we ask Twitter for long lasting token credentials. These are specific to the application and user and will act like password to make future requests.
            $token_credentials = $connection->getAccessToken($_REQUEST['oauth_verifier']);

            Here we can from our token credentials back to the application (i'd suggest a more secure method of sending these than encoded JSON. but here for example):
            return array($token_credentials);
        })

从这里,使用令牌凭证,我们构建一个新的 TwitterOAuth 对象。

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token_credentials['oauth_token'],
$token_credentials['oauth_token_secret']);

并作为用户进行认证请求

$status = $connection->post('statuses/update', array('status' => 'Text of status here', 'in_reply_to_status_id' => 123456));

许可