procorbin / bird-elephant
一个简单的库,用于从Twitter APIv2端点请求数据
资助包维护!
danieldevine
Requires
- php: ^7.3
- ext-json: *
- guzzlehttp/guzzle: ^6.0
- guzzlehttp/oauth-subscriber: ^0.6.0
- league/oauth2-client: ^2.6
Requires (Dev)
- league/oauth1-client: ^1.10
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.1
- vlucas/phpdotenv: ^5.2
README
在PHP中连接到Twitter API v2端点。
此包提供多种方式与PHP中的Twitter Rest API v2端点交互。它提供了一组干净且易于理解的函数和方法,用于发送推文、管理用户、查找数据以及Twitter API v2提供的所有其他功能,这些功能可以在您的应用程序或网站内部使用。
入门
要使用Twitter API v2,以及此包,您必须拥有已批准的开发者账户,并且已激活新的开发者门户。
了解更多关于获取访问Twitter API v2端点的方法
安装
通过composer安装。
composer require procorbin/bird-elephant
认证
在开发门户中创建应用程序时,您需要生成您的凭据。
遵循上述Twitter开发者文档中的说明进行此操作。请确保授予您的应用程序正确的权限,并且如果需要,启用3-legged OAuth。
以下是将凭据作为键值数组传递的方式
$credentials = array( //these are values that you can obtain from developer portal: 'consumer_key' => xxxxxx, // identifies your app, always needed 'consumer_secret' => xxxxxx, // app secret, always needed 'bearer_token' => xxxxxx, // OAuth 2.0 Bearer Token requests //this is a value created duting an OAuth 2.0 with PKCE authentication flow: 'auth_token' => xxxxxx // OAuth 2.0 auth token //these are values created during an OAuth 1.0a authentication flow to act ob behalf of other users, but these can also be obtained for your app from the developer portal in order to act on behalf of your app. 'token_identifier' => xxxxxx, // OAuth 1.0a User Context requests 'token_secret' => xxxxxx, // OAuth 1.0a User Context requests ); $twitter = new BirdElephant($credentials);
OAuth 2.0 Bearer token认证是最直接的,但会限制您访问某些端点。
当然,在两种可能的用户上下文认证流程中,您都需要传递认证用户的凭据作为token_identifier和token_secret(对于OAuth 1.0a)或'auth_token'(对于OAuth 2.0)。
OAuth 1.0a受支持,但新应用程序最好优先选择带有PKCE的OAuth 2.0,因为某些较新的端点只支持这种认证形式,并且Twitter可能会在未来取消对此的支持。
OAuth 1.0a是执行媒体上传所必需的 - BirdElephant作为v2替代品支持的唯一Api v1.1端点尚未存在。
您可以查看index.php和authenticate.php,以了解简单的OAuth 2.0与PKCE流程在实际中的工作方式。为此,请使用专门的oAuth库 - 在示例中,我使用了smolblog/oauth2-twitter,它做得很好。
记住在使用带有PKCE的OAuth 2.0时包含必要的权限 - 完整列表在这里
https://developer.twitter.com/en/docs/authentication/oauth-2-0/authorization-code
请小心保护您的凭据,并永远不要将它们提交到您的仓库。我建议使用.env文件来管理您的凭据,您可以将.env.example的内容复制到项目中的.env,并根据自己的凭据进行填充: 如何使用它在这里
文档
在此处可以找到所有可用Bird Elephant方法的文档和示例。
快速示例
该包提供了多种与Twitter API交互的方式。推荐的方式是通过使用简单的辅助方法,但也可以使用实用方法,以及直接访问许多底层类。如果您希望与底层类交互,请阅读代码中的文档。
use Procorbin\BirdElephant\BirdElephant; //your credentials, should be passed in via $_ENV or similar, don't hardcode. $credentials = array( 'consumer_key' => xxxxxx, 'consumer_secret' => xxxxxx, 'bearer_token' => xxxxxx, // if using oAuth 2.0 with PKCE 'auth_token' => xxxxxx // OAuth 2.0 auth token //if using oAuth 1.0a 'token_identifier' => xxxxxx, 'token_secret' => xxxxxx, ); //instantiate the object $twitter = new BirdElephant($credentials); //get a user's followers using the handy helper methods $followers = $twitter->user('coderjerk')->followers(); //pass your query params to the methods directly $following = $twitter->user('coderjerk')->following([ 'max_results' => 20, 'user.fields' => 'profile_image_url' ]); //tweet something $tweet = (new \Procorbin\BirdElephant\Compose\Tweet)->text(".@coderjerk is so cool"); $twitter->tweets()->tweet($tweet); // You can also use the sub classes / methods directly if you like: $user = new UserLookup($credentials); $user = $user->getSingleUserByID('2244994945', null);
参考
注释
这是一个由我利用业余时间编写的非官方工具,与Twitter无关。
此包不支持Twitter API v1.1(媒体上传除外)。
赞助商
如果您或您的公司觉得这个库很有用,请通过给我一些欧元表示支持,我会在这里和在项目网站上向您致谢。
贡献
克隆/下载代码并运行
composer install
将.env.example
复制到.env
,并添加您的测试凭证。
要运行测试
./vendor/bin/phpunit
欢迎提出问题、拉取请求和其他贡献。请使用提供的模板。