Skip to content

PHP package providing easy and fast access to Twitter API V2.

License

Notifications You must be signed in to change notification settings

cibles/twitter-api-v2-php

ďż˝
ďż˝

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ďż˝

History

84 Commits
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝
ďż˝

Repository files navigation

Twitter API V2 for PHP

PHP Badge Twitter Run Tests MIT Licensed last version Downloads twitter

Twitter API V2 is a PHP package which provides an easy and fast access to Twitter REST API for Version 2 endpoints.

Documentation

Installation

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.

Github Actions

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.

How to use

Active your developer 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.

Prepare settings

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);

Tweets endpoints

Timeline endpoints

Find Recent Mentioning for a User

Example:

$return = $client->timeline()->getRecentMentions($accountId)->performRequest();

Find Recent Tweets for a User

Example:

$return = $client->timeline()->getRecentTweets($accountId)->performRequest();

Reverse Chronological Timeline by user ID

Example:

$return = $client->timeline()->getReverseChronological()->performRequest();

Tweet/Likes endpoints

Tweets liked by a user

Example:

$return = $client->tweetLikes()->addMaxResults($pageSize)->getLikedTweets($accountId)->performRequest();

Users who liked a tweet

Example:

$return = $client->tweetLikes()->addMaxResults($pageSize)->getUsersWhoLiked($tweetId)->performRequest();

Tweet/Lookup endpoints

Search specific tweets

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()
;

Find all replies from a Tweet

->addFilterOnConversationId($tweetId);

Tweet endpoints

Fetch a tweet by Id

Example:

$return = $client->tweet()->->fetch(1622477565565739010)->performRequest();

Create a new Tweet

Example:

$return = $client->tweet()->create()->performRequest(['text' => 'Test Tweet... ']);

Tweet/Quotes endpoints

Returns Quote Tweets for a Tweet specified by the requested Tweet ID

Example:

$return = $client->tweetQuotes()->getQuoteTweets($tweetId)->performRequest();

Retweet endpoints

Retweet a Tweet

Example:

$return = $client->retweet()->performRequest(['tweetId' => $tweet_id]);

Tweet/Replies endpoints

Hide a reply to a Tweet

Example:

$return = $client->->tweetReplies()->hideReply($tweetId)->performRequest(['hidden' => true]);

Unhide a reply to a Tweet

Example:

$return = $client->->tweetReplies()->hideReply($tweetId)->performRequest(['hidden' => false]);

Tweet/Bookmarks endpoints

Lookup a user's Bookmarks

Example:

$return = $client->tweetBookmarks()->lookup()->performRequest();

Users endpoints

User/Blocks endpoints

Retrieve the users which you've blocked

Example:

$return = $client->userBlocks()->lookup()->performRequest();

User/Follows endpoints

Retrieve the users which are following you

Example:

$return = $client->userFollows()->getFollowers()->performRequest();

Retrieve the users which you are following

Example:

$return = $client->userFollows()->getFollowing()->performRequest();

User/Lookup endpoints

Find Twitter Users

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()
;

User/Mutes endpoints

Retrieve the users which you've muted

Example:

$return = $client->userMutes()->lookup()->performRequest();

Mute user by username or ID

Example:

$return = $client->userMutes()->mute()->performRequest(['target_user_id' => $userId]);

Unmute user by username or ID

Example:

$return = $client->userMutes()->unmute()->performRequest(['target_user_id' => $userId]);

User/Follows endpoints

Follow a user

Example:

$return = $client->userFollows()->follow()->performRequest(['target_user_id' => $userId]);

Unfollow a user

Example:

$return = $client->userFollows()->unfollow($userId)->performRequest(['target_user_id' => self::$userId]);

Contributing

Fork/download the code and run

composer install

copy test/config/.env.example to test/config/.env and add your credentials for testing.

To run tests

./vendor/bin/phpunit

To run code analyzer

./vendor/bin/phpstan analyse .

About

PHP package providing easy and fast access to Twitter API V2.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%