Twitter API V2 is a PHP package which provides an easy and fast access to Twitter REST API for Version 2 endpoints.
- Installation
- Github Actions
- How to use
- Tweets endpoints
- Users endpoints
- Contributing
First, you need to add the component to your composer.json
composer require noweh/twitter-api-v2-php
Update your packages with composer update or install with composer install.
This repository uses Github Actions for each push/pull request with PHPStan/PHPUnit.
Therefore, for each valid push, a new Tweet is posted from my Twitter test account.
Firstly, you need to follow this tutorial.
- Request of an approved account;
- Once you have an approved developer account, you will need to create a Project;
- Enable read/write access for your Twitter app;
- Generate Consumer Keys and Authentication Tokens;
- Grab your Keys and Tokens from the twitter developer site.
Settings are expected as below:
use Noweh\TwitterApi\Client;
$settings['account_id']
$settings['access_token'],
$settings['access_token_secret'],
$settings['consumer_key'],
$settings['consumer_secret'],
$settings['bearer_token']
$client = new Client($settings);
Example:
$return = $client->timeline()->getRecentMentions($accountId)->performRequest();
Example:
$return = $client->timeline()->getRecentTweets($accountId)->performRequest();
Example:
$return = $client->timeline()->getReverseChronological()->performRequest();
Example:
$return = $client->tweetLikes()->addMaxResults($pageSize)->getLikedTweets($accountId)->performRequest();
Example:
$return = $client->tweetLikes()->addMaxResults($pageSize)->getUsersWhoLiked($tweetId)->performRequest();
Example:
$return = $client->tweetLookup()
->showMetrics()
->onlyWithMedias()
->addFilterOnUsernamesFrom([
'twitterdev',
'Noweh95'
], TweetLookup::OPERATORS['OR'])
->addFilterOnKeywordOrPhrase([
'Dune',
'DenisVilleneuve'
], TweetLookup::OPERATORS['AND'])
->addFilterOnLocales(['fr', 'en'])
->showUserDetails()
->performRequest()
;
$client->tweetLookup()
->addMaxResults($pageSize)
->addFilterOnKeywordOrPhrase($keywordFilter)
->addFilterOnLocales($localeFilter)
->showUserDetails()
->showMetrics()
->performRequest()
;
->addFilterOnConversationId($tweetId);
Example:
$return = $client->tweet()->->fetch(1622477565565739010)->performRequest();
Example:
$return = $client->tweet()->create()->performRequest(['text' => 'Test Tweet... ']);
Example:
$return = $client->tweetQuotes()->getQuoteTweets($tweetId)->performRequest();
Example:
$return = $client->retweet()->performRequest(['tweetId' => $tweet_id]);
Example:
$return = $client->->tweetReplies()->hideReply($tweetId)->performRequest(['hidden' => true]);
Example:
$return = $client->->tweetReplies()->hideReply($tweetId)->performRequest(['hidden' => false]);
Example:
$return = $client->tweetBookmarks()->lookup()->performRequest();
Example:
$return = $client->userBlocks()->lookup()->performRequest();
Example:
$return = $client->userFollows()->getFollowers()->performRequest();
Example:
$return = $client->userFollows()->getFollowing()->performRequest();
findByIdOrUsername()
expects either an array, or a string.
You can specify the search mode as a second parameter (Client::MODES['USERNAME']
OR Client::MODES['ID']
)
Example:
$return = $client->userLookup()
->findByIdOrUsername('twitterdev', UserSearch::MODES['USERNAME'])
->performRequest()
;
Example:
$return = $client->userMutes()->lookup()->performRequest();
Example:
$return = $client->userMutes()->mute()->performRequest(['target_user_id' => $userId]);
Example:
$return = $client->userMutes()->unmute()->performRequest(['target_user_id' => $userId]);
Example:
$return = $client->userFollows()->follow()->performRequest(['target_user_id' => $userId]);
Example:
$return = $client->userFollows()->unfollow($userId)->performRequest(['target_user_id' => self::$userId]);
Fork/download the code and run
composer install
copy test/config/.env.example
to test/config/.env
and add your credentials for testing.
./vendor/bin/phpunit
./vendor/bin/phpstan analyse .