PHP code example of abdulbaquee / twitter-oauth-v2
1. Go to this page and download the library: Download abdulbaquee/twitter-oauth-v2 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Abdulbaquee\TwitterOAuth\Session\NativeSessionHandler;
$sessionHandler = new NativeSessionHandler();
use Abdulbaquee\TwitterOAuth\Session\LaravelSessionHandler;
use Illuminate\Session\Store;
$sessionHandler = new LaravelSessionHandler(app('session.store'));
use Abdulbaquee\TwitterOAuth\Session\SymfonySessionHandler;
use Symfony\Component\HttpFoundation\Session\Session;
$sessionHandler = new SymfonySessionHandler($session);
use Abdulbaquee\TwitterOAuth\Session\SessionHandlerInterface;
class CustomSessionHandler implements SessionHandlerInterface
{
// Implement the
use Abdulbaquee\TwitterOAuth\OAuthClient;
use Abdulbaquee\TwitterOAuth\TwitterApi;
use Abdulbaquee\TwitterOAuth\Session\NativeSessionHandler;
// Load configuration
$config = $config['twitter']['client_secret'],
$config['twitter']['redirect_uri'],
$config['twitter']['scopes'],
null,
$sessionHandler
);
// Get authorization URL
$authUrl = $oauthClient->getAuthorizationUrl();
// Redirect user to $authUrl
// After user authorization, exchange code for tokens
$tokens = $oauthClient->getAccessToken($_GET['code']);
// Initialize Twitter API client
$twitterApi = new TwitterApi($tokens['access_token']);
// Get user profile
$userProfile = $twitterApi->getUserProfile();
// Get user tweets
$userTweets = $twitterApi->getUserTweets($userId);
// Get user followers
$followers = $twitterApi->getUserFollowers($userId);
// Get user following
$following = $twitterApi->getUserFollowing($userId);
// Get user mentions
$mentions = $twitterApi->getUserMentions($userId);
// Get user likes
$likes = $twitterApi->getUserLikes($userId);
// Create a tweet
$tweet = $twitterApi->createTweet('Hello, Twitter!');
// Create a tweet with media
$mediaId = $twitterApi->uploadMedia('/path/to/image.jpg');
$tweet = $twitterApi->createTweet('Check out this image!', [$mediaId]);
// Delete a tweet
$twitterApi->deleteTweet($tweetId);
// Like a tweet
$twitterApi->likeTweet($userId, $tweetId);
// Unlike a tweet
$twitterApi->unlikeTweet($userId, $tweetId);
// Retweet
$twitterApi->retweet($userId, $tweetId);
// Remove retweet
$twitterApi->removeRetweet($userId, $tweetId);
// Reply to tweet
$twitterApi->replyToTweet('This is a reply!', $tweetId);
// Create a list
$list = $twitterApi->createList('My List', 'Description', false);
// Get user lists
$lists = $twitterApi->getUserLists($userId);
// Get list members
$members = $twitterApi->getListMembers($listId);
// Get list tweets
$tweets = $twitterApi->getListTweets($listId);
// Add member to list
$twitterApi->addListMember($listId, $userId);
// Remove member from list
$twitterApi->removeListMember($listId, $userId);
// Delete list
$twitterApi->deleteList($listId);
// Follow user
$twitterApi->followUser($sourceUserId, $targetUserId);
// Unfollow user
$twitterApi->unfollowUser($sourceUserId, $targetUserId);
// Block user
$twitterApi->blockUser($sourceUserId, $targetUserId);
// Unblock user
$twitterApi->unblockUser($sourceUserId, $targetUserId);
// Mute user
$twitterApi->muteUser($sourceUserId, $targetUserId);
// Unmute user
$twitterApi->unmuteUser($sourceUserId, $targetUserId);
use Abdulbaquee\TwitterOAuth\Examples\BaseExample;
use Abdulbaquee\TwitterOAuth\OAuthClient;
$client = new OAuthClient(
'your_client_id',
'your_client_secret',
'your_redirect_uri',
['tweet.read', 'tweet.write', 'users.read']
);
// Create example instance
$example = new BaseExample($client);
// Get user information
$userInfo = $example->getUserInfo();
// Create a tweet
$tweet = $example->createTweet('Hello, Twitter!');
// Upload media
$media = $example->uploadMedia('/path/to/image.jpg', 'image/jpeg');
// Delete a tweet
$example->deleteTweet('tweet_id');
class CustomExample extends BaseExample
{
public function getTweetsByUser(string $userId): array
{
return $this->get("https://api.twitter.com/2/users/{$userId}/tweets", [
'max_results' => 100,
'tweet.fields' => 'created_at,public_metrics'
]);
}
}