abhishekbhardwaj / twitter-api-php
Twitter v1.1 API的简单PHP包装器。使用Guzzle!
v1.1.5
2014-12-02 06:04 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~5.0
- guzzlehttp/oauth-subscriber: ~0.2
README
Twitter-API-PHP是Twitter API v1.1调用的简单PHP包装库。它旨在使开发者(在服务器或客户端)尽可能简单易用地使用Twitter API。
它支持
安装
要通过Composer安装,请在您的composer.json文件中添加以下内容并运行composer install
{
"require": {
"abhishekbhardwaj/twitter-api-php": "~1.1"
}
}
依赖项
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~5.0",
"guzzlehttp/oauth-subscriber": "~0.2"
基本用法
一些示例位于examples/
目录中
- appExample.php:仅应用认证示例
- userExample.php:三重认证授权示例
详细信息请见下文
要将Twitter用作应用
- 使用适当的API密钥创建
Twitter\Config\AppCredentials
类的实例。
$credentials = new AppCredentials($consumerKey, $consumerSecret);
- 使用您刚刚创建的
AppCredentials
对象创建Twitter\Client
实例。
$client = new Client($credentials);
- 创建Twitter的应用连接。
$app = $client->connect();
- 当将Twitter用作应用时,您需要使用Bearer Token,这可以通过调用
createBearerToken()
函数生成。
$app->createBearerToken(); //to access bearer token (for storing in db for later use), you can use: $app->getCredentials()->getBearerToken();
- 现在您可以向Twitter发送GET请求(因为作为应用您只能从Twitter获取),[详细信息见此处!]{https://dev.twitter.com/oauth/application-only}
//getting @abhishekwebin's recent timeline. $response = $app->get('users/show.json', array('screen_name' => 'abhishekwebin'));
- 由于我们使用Guzzle与Twitter通信,因此
$response
对象是GuzzleHttp\Message\ResponseInterface
的实例,因此它具有许多有用的函数,例如
$response->getHeaders(); //gets the response headers $response->getStatusCode(); //gets the status code $response->json(); //parse response as JSON and return decoded data as assoc-array $response->getBody(); //gets the response body
有关Guzzle的更多信息,请参阅其官方文档此处。
要将Twitter用作用户
本节展示了如何作为用户登录
- 使用适当的API密钥创建
Twitter\Config\UserCredentials
类的实例。
$credentials = new UserCredentials($consumerKey, $consumerSecret, $callbackUrl);
- 使用您刚刚创建的
UserCredentials
对象创建Twitter\Client
实例。
$client = new Client($credentials);
- 创建Twitter的用户连接。
$app = $client->connect();
- 获取授权URL。
$redirectUrl = $app->getRedirectUrlForAuth();
-
将用户重定向到该URL。
-
用户登录后,Twitter将用户重定向回您在应用设置和上述凭据中指定的回调URL。
-
回调URL将获得一个临时的oauthToken和oauthVerifier作为查询参数。使用它们生成访问令牌(令牌和密钥)
$accessTokens = $app->getAccessToken($oauthToken, $oauthVerifier);
-
$accessTokens
是一个包含oauth_token
和oauth_token_secret
的关联数组,这些令牌基本上不会过期。您可以将这些存储在数据库中。 -
现在您可以代表经过身份验证的用户向端点发送GET或POST请求。
从端点获取GET
//getting @abhishekwebin's recent timeline. $response = $app->get('users/show.json', array('screen_name' => 'abhishekwebin'));
向端点POST
//post a new tweet: 'Test status!' as the currently authenticated users. $response = $app->post('statuses/update.json', array('status' => 'Test status!'));
在推文中上传图片
最多可以附加4张图片到一条推文。要这样做
//list of pictures to upload. Input array items shouldn't be more than 4. $media = $app->uploadMedia(array( '{{FULL PATH TO PICTURE}}', '{{FULL PATH TO PICTURE}}', '{{FULL PATH TO PICTURE}}', '{{FULL PATH TO PICTURE}}' )); //post a new status $response = $app->post('statuses/update.json', array( 'status' => 'Test status!', 'media_ids' => $media ));
传递给uploadMedia()
的图片列表可以是URL或绝对文件路径。
待办事项
- 将命名空间更改为除了
Twitter\*
之外的内容 - 更多文档。
变更日志
- 2014年11月12日:
版本1.1.2
- GET请求中的参数现在是可选的。 - 2014年11月9日:
版本1.1.*
- 更稳定,还允许您将图片上传到Twitter。 - 2014年11月8日:
版本1.0.*
- 库的第一个版本,不支持媒体上传。
重要链接
贡献
如果您发现任何错误,无论是提交问题还是拉取请求,都总是受欢迎! :)
许可证
本库采用MIT许可证。有关详细信息,请参阅 LICENSE
文件!