traviswimer / twittering
Twitter oAuth和API请求库,语法简单易用。
dev-master
2014-06-07 03:40 UTC
Requires
- php: >=5.3.2
- ruudk/twitter-oauth: *
Requires (Dev)
- phpunit/phpunit: 4.0.*
This package is not auto-updated.
Last update: 2024-09-24 01:19:47 UTC
README
PHP类,用于简单的Twitter oAuth令牌请求、身份验证和API v1.1请求。(基于TwitterOAuth包构建)
入门
使用Composer安装,将其添加到您的composer.json文件中
"require": { "traviswimer/twittering": "dev-master" }
然后运行compsoser install或php composer.phar install,具体取决于您的配置。
示例用法
// Include Composer autoloader require_once __DIR__.'/vendor/autoload.php'; // First create initialize a Twittering object with you API key/secret $twittering = new \Twittering\Twittering(array( "api_key" => "YOUR_CONSUMER_KEY", "api_secret" => "YOUR_CONSUMER_SECRET", )); // Then the requestTokens() method can be used to redirect the user to Twitter // for authentication. The user will then be sent back to the URL of your script. // This time, when requestTokens() is called, it will detect the $_GET parameters // sent from Twitter and continue with the authorization process. $tokens = $twittering->requestTokens(); // Now that you have the tokens, you can save them to a database for future use. // To make API requests, call the authenticate() method. $apiConnection = $twittering->authenticate( $tokens ); // You are now ready to make API requests $timeline = $apiConnection->get( 'statuses/home_timeline', array('count' => '10') );
文档
new \Twittering\Twittering( $api_auth_info, );
- $api_auth_info - 包含oAuth信息的数组。
- "api_key" - 您的应用程序消费者密钥
- "api_secret" - 您的应用程序消费者密钥
requestTokens( $callback_url, $redirect );
如果设置了$_GET变量oauth_token和oauth_verifier,此方法的行为将有所不同
没有$_GET变量(初始请求)
这将使用户前往Twitter授权您的应用程序。
- $callback_url(默认:当前URL)- String。Twitter在用户授权您的应用程序后应将其重定向回的URL。
- $redirect(默认:true)- Boolean。确定用户是否应自动重定向到Twitter以授权您的应用程序。如果设置为false,则
requestTokens()将返回您应将用户发送到的URL。
有$_GET变量(Twitter回调请求)
这将生成用户的长期令牌。
在此第二次传递期间不需要参数。这将返回一个包含用户长期令牌的数组,可以存储在您的数据库中。您还可以直接将此数组传递给authenticate()方法。
authenticate( $tokens );
此方法创建一个对象,代表已认证用户与Twitter API进行交互。
- $tokens - array。包含用户令牌的关联数组。应包含从
requestTokens()方法获得的以下键- oauth_token
- oauth_token_secret
贡献
虽然没有正式的风格指南,但请务必维护现有的编码风格。为任何新的或更改的功能添加单元测试。