PHP code example of noweh / twitter-api-v2-php

1. Go to this page and download the library: Download noweh/twitter-api-v2-php 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/ */

    

noweh / twitter-api-v2-php example snippets


use Noweh\TwitterApi\Client;

$settings = [
    'account_id' => 'YOUR_ACCOUNT_ID',
    'access_token' => 'YOUR_ACCESS_TOKEN',
    'access_token_secret' => 'YOUR_TOKEN_SECRET',
    'consumer_key' => 'YOUR_CONSUMER_KEY',
    'consumer_secret' => 'YOUR_CONSUMER_SECRET',
    'bearer_token' => 'YOUR_BEARER_TOKEN',
    'free_mode' => false, // Optional
    'api_base_uri' => 'https://api.twitter.com/2/', // Optional
]; 

$client = new Client($settings);

$response = $this->client->tweet()->create()
    ->performRequest([
        'text' => 'Test Tweet... '
    ],
    withHeaders: true)
;

/*
Output:
object(stdClass)#399 (2) {
    ["data"]=>
    object(stdClass)#398 (3) {
        ["edit_history_tweet_ids"]=>
        array(1) {
            [0]=>
            string(19) "1690304934837637121"
        }
        ["id"]=>
        string(19) "1690304934837637121"
        ["text"]=>
        string(39) "Test Tweet..."
    }
    ["headers"]=>
        ...
        ["x-rate-limit-limit"]=>
        array(1) {
            [0]=>
            string(5) "40000"
        }
        ["x-rate-limit-reset"]=>
        array(1) {
            [0]=>
            string(10) "1691835953"
        }
        ["x-rate-limit-remaining"]=>
        array(1) {
            [0]=>
            string(5) "39998"
        }
        ...
    }
}
*/

...
$settings['free_mode'] = true;
$client = new Client($settings);

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

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

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

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

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

$return = $client->tweetLookup()
    ->showMetrics()
    ->onlyWithMedias()
    ->addFilterOnUsernamesFrom([
        'twitterdev',
        'Noweh95'
    ], \Noweh\TwitterApi\TweetLookup::OPERATORS['OR'])
    ->addFilterOnKeywordOrPhrase([
        'Dune',
        'DenisVilleneuve'
    ], \Noweh\TwitterApi\TweetLookup::OPERATORS['AND'])
    ->addFilterOnLocales(['fr', 'en'])
    ->showUserDetails()
    ->performRequest()
;

$client->tweetLookup()
    ->addMaxResults($pageSize)
    ->addFilterOnKeywordOrPhrase($keywordFilter)
    ->addFilterOnLocales($localeFilter)
    ->showUserDetails()
    ->showMetrics()
    ->performRequest()
;

->addFilterOnConversationId($tweetId);

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

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

$file_data = base64_encode(file_get_contents($file));
$media_info = $client->uploadMedia()->upload($file_data);
$return = $client->tweet()->create()
    ->performRequest([
        'text' => 'Test Tweet... ', 
        "media" => [
            "media_ids" => [
                (string)$media_info["media_id"]
            ]
        ]
    ])
;

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

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

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

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

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

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

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

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

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

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

$return = $client->userMeLookup()->performRequest();

$return = $client->userLookup()
    ->findByIdOrUsername('twitterdev', \Noweh\TwitterApi\UserLookup::MODES['USERNAME'])
    ->performRequest()
;

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

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

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