zaruto / bird-elephant
一个简单的库,用于从Twitter APIv2端点请求数据
支持包维护!
danieldevine
Requires
- guzzlehttp/guzzle: ^7.0
- guzzlehttp/oauth-subscriber: ^0.6.0
Requires (Dev)
- league/oauth1-client: ^1.10
- phpunit/phpunit: ^9.5
- smolblog/oauth2-twitter: ^1.0
- 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 coderjerk/bird-elephant
身份验证
在开发者门户创建您的应用程序时,您将需要生成您的凭证。
遵循上述Twitter开发者文档中关于如何操作的解释。请确保授予您的应用程序正确的权限,并根据需要启用三腿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是执行媒体上传所必需的 - 作为v2替代品的BirdElephant支持的唯一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的内容复制到项目中,并根据自己的凭证填充它:如何使用它在这里
文档
所有可用的Bird Elephant方法的文档和示例可以在这里找到。
快速示例
本包提供多种与Twitter API交互的方式。推荐使用简单的辅助方法,但也提供了工具方法,并且可以直接访问许多底层类。如果您想与底层类交互,请阅读代码中的文档。
use Coderjerk\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 \Coderjerk\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);
参考
注意
这是一个由我(me)在业余时间编写的非官方工具,并且与Twitter无任何关联。
此包不支持Twitter API v1.1(媒体上传除外)。
赞助商
感谢Hype Machine对本项目的赞助。
赞助商
如果您或您的公司认为这个库很有用,可以通过给我一些欧元来表达您的支持,并且我会在这里和项目网站上向您表示感谢。
贡献
分叉/下载代码并运行
composer install
将.env.example
复制到.env
,并添加您的测试凭证。
要运行测试
./vendor/bin/phpunit
欢迎提交问题、拉取请求和其他贡献。请使用提供的模板。
如果您想参与,可以查看项目板上的即将推出功能 :)