PHP code example of miladrahimi / twitter-bot

1. Go to this page and download the library: Download miladrahimi/twitter-bot 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/ */

    

miladrahimi / twitter-bot example snippets


use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
// Your bot is ready!

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
$response = $bot->api('GET', 'search/tweets.json', ['q' => '#pink_floyd']);

if ($response->status() == 200) {
    print_r($response->content());
} else {
    echo $response; // JSON
}

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
$bot->setOAuthToken(TOKEN, TOKEN_SECRET);
$response = $bot->api('POST', 'statuses/update.json', ['status' => 'Hello from bot!']);

print_r($response->content());

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
$bot->setOAuthToken(TOKEN, TOKEN_SECRET);
$response = $bot->apiJson('POST', 'direct_messages/events/new.json', [
    'event' => [
        'type' => 'message_create',
        'message_create' => [
            'target' => [
                'recipient_id' => 666,
            ],
            'message_data' => [
                'text' => 'Hello from bot!',
            ]
        ]
    ]
]);

print_r($response->content());

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
$bot->setOAuthToken(TOKEN, TOKEN_SECRET);

$file = __DIR__ . '/files/pink-floyd.jpg';
$mediaResponse = $bot->upload($file, 'POST', 'media/upload.json', [
    'media_category' => 'tweet_image',
    'media_type' => 'image/jpeg',
]);

$response = $bot->api('POST', 'statuses/update.json', [
    'status' => 'Hello from bot!',
    'media_ids' => $mediaResponse->content()['media_id'],
]);

print_r($response->content());

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);

$response = $bot->oauth('POST', 'oauth/request_token', [
    'oauth_callback' => 'https://your-app.com/twitter/callback',
]);

if ($response->status() == 200) {
    $token = $response->content()['oauth_token'];
    $tokenSecret = $response->content()['oauth_token_secret'];
    
    // Generate twitter redirection url
    $url = $bot->oauthUrl($token);

    // Redirect user to $url
}

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);

$response = $bot->oauth('POST', 'oauth/access_token', [
    'oauth_token' => $_REQUEST['oauth_token'],
    'oauth_verifier' => $_REQUEST['oauth_verifier'],
]);

print_r($response->content()); // oauth_token, oauth_token_secret, screen_name, ...

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);

$bot->getClient()->setTimeout(13);