Twitter API V2 is a PHP package which provides an easy and fast access to Twitter REST API for Version 2 endpoints.
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' => 1234567,
'consumer_key' => 'CONSUMER_KEY',
'consumer_secret' => 'CONSUMER_SECRET',
'bearer_token' => 'BEARER_TOKEN',
'access_token' => 'ACCESS_TOKEN',
'acess_token_secret' => 'ACCESS_TOKEN_SECRET'
];
$client = new Client($settings);
Example:
$return = $client->tweetSearch()
->showMetrics()
->onlyWithMedias()
->addFilterOnUsernamesFrom([
'twitterdev',
'Noweh95'
], \Noweh\TwitterApi\Enum\Operators::or)
->addFilterOnKeywordOrPhrase([
'Dune',
'DenisVilleneuve'
], \Noweh\TwitterApi\Enum\Operators::and)
->addFilterOnLocales(['fr', 'en'])
->showUserDetails()
->performRequest()
;
findByIdOrUsername()
expects either an array, or a string.
You can specify the search mode as a second parameter:
->findByIdOrUsername('twitterdev', \Noweh\TwitterApi\Enum\Modes::username) //OR \Noweh\TwitterApi\Enum\Modes::id
Example:
$return = $client->tweet()->performRequest('POST', ['text' => 'This is a test....']);
Example:
$return = $client->retweet()->performRequest('POST', ['tweet_id' => $tweet->id]);
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 .