coderjerk/elephant-bird

此包已被废弃且不再维护。作者建议使用coderjerk/bird-elephant包。

一个简单的库,用于从Twitter APIv2端点请求数据

v1.5.1 2021-01-03 09:54 UTC

This package is auto-updated.

Last update: 2021-11-04 14:33:50 UTC


README

自2021年11月起已弃用 - 请使用Bird Elephant,它改进了很多! 从现在起,它将仅接收安全补丁。

注意:此包目前仅支持基于载体令牌的应用访问。如果您想讨论,请在Twitter上关注@coderjerk

这些端点是早期访问,可能会更改。此包不支持旧的v1.1端点。

当前支持

  • API v2
    • 推文
      • 最近搜索
      • 查找
      • 过滤流(基本支持)
      • 时间线
    • 用户
      • 查找关注
      • 查找用户

要使用此包,您必须拥有已批准的开发者账户,并已激活新的开发者门户。

了解更多关于获取Twitter API v2端点的访问权限

Twitter入门文档

API参考

Twitter最近搜索端点API参考

查找多条推文API参考

查找单个推文API参考

请注意,当前操作符支持相当稀疏,这使推文和媒体在某些上下文中的使用风险很大 - 例如,过滤NSFW内容目前尚不可行。我不知道这是否在Twitter的计划之中。

安装

通过composer安装。

$ composer require coderjerk/elephant-bird

认证

目前只支持载体令牌。将.env.example的内容复制到项目中的.env,并用您在Twitter开发者门户中为项目设置的凭据填充。如果您在项目中不使用.env,您将需要设置它,详情在此

示例

最近搜索

搜索与足球相关的14条最新推文。

use Coderjerk\ElephantBird\RecentSearch;

$params = [
    'query'        => 'football',
    'max_results'  => 14,
];

$search = new RecentSearch;
$result = $search->RecentSearchRequest($params);

$tweets = $result->data;

搜索媒体

use Coderjerk\ElephantBird\RecentSearch;

$params = [
    'query' => 'dancing has:images ',
    'tweet.fields' => 'attachments,author_id,created_at',
    'expansions'   => 'attachments.media_keys',
    'media.fields' => 'public_metrics,type,url,width',
    'max_results'  => 10,
];

$search = new RecentSearch;
$result = $search->RecentSearchRequest($params);
$media = $result->includes->media;

推文查找

通过ID查找多条推文的详细信息 - 如果提供单个ID,Elephant Bird将选择单个推文端点

use Coderjerk\ElephantBird\TweetLookup;

$ids = [
    '1261326399320715264',
    '1278347468690915330'
];

$params = [
    'tweet.fields' => 'attachments,author_id,created_at,public_metrics,source'
];

$lookup = new TweetLookup;
$tweets = $lookup->getTweetsById($ids, $params);

时间线

获取指定用户的推文。

use Coderjerk\ElephantBird\TimeLine;

$timeline = new TimeLine;

$params = [
    'tweet.fields' => 'attachments,author_id,created_at,public_metrics,source'
];

$tweets = $timeline->getTweets('802448659', $params);

获取指定用户的提及。

use Coderjerk\ElephantBird\TimeLine;

$timeline = new TimeLine;

$params = [
    'tweet.fields' => 'attachments,author_id,created_at,public_metrics,source'
];

$mentions = $timeline->getMentions('802448659', $params);

用户查找

通过用户名查找单个用户

use Coderjerk\ElephantBird\UserLookup;

$params = [
    'user.fields' => 'id'
];

$usernames = [
    'coderjerk'
];

$userLookup = new UserLookup;
$user = $userLookup->lookupUsersByUsername($usernames, $params);

通过ID查找多个用户

use Coderjerk\ElephantBird\UserLookup;

$ids = [
    '802448659',
    '16298441'
];

$userLookup = new UserLookup;
$user = $userLookup->lookupUsersById($ids, $params);

关注者查找

获取关注者

use Coderjerk\ElephantBird\FollowsLookup;

$follows = new FollowsLookup;

$params = [
    'tweet.fields' => 'attachments,author_id,created_at,public_metrics,source'
];

$followers = $follows->getFollowers('802448659', $params);

获取关注列表

use Coderjerk\ElephantBird\FollowsLookup;


$follows = new FollowsLookup;

$params = [
    'tweet.fields' => 'attachments,author_id,created_at,public_metrics,source'
];

$following = $follows->getFollowing('802448659', $params);

贡献

Fork/下载代码并运行 composer install

本软件包处于开发初期。欢迎提出问题、发起拉取请求和其他贡献。

您可以在此查看项目板,了解即将推出的功能: