PHPackages                             miladrahimi/twitter-bot - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. miladrahimi/twitter-bot

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

miladrahimi/twitter-bot
=======================

A lightweight and clean implementation of Twitter bot API

v1.3.5(3y ago)9351MITPHPPHP &gt;=7.1

Since Dec 17Pushed 3y ago1 watchersCompare

[ Source](https://github.com/miladrahimi/twitter-bot)[ Packagist](https://packagist.org/packages/miladrahimi/twitter-bot)[ Docs](https://github.com/miladrahimi/twitter-bot)[ RSS](/packages/miladrahimi-twitter-bot/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (7)DependenciesVersions (8)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/952f47ec2e3a28f6fa4e2b67135cdeee5d44d43e58d8974ac8a7acdd00b8e878/68747470733a2f2f706f7365722e707567782e6f72672f6d696c6164726168696d692f747769747465722d626f742f762f737461626c65)](https://packagist.org/packages/miladrahimi/twitter-bot)[![Total Downloads](https://camo.githubusercontent.com/4c954f36f3bd64bf9f529bbbc1d801d75052c05382bb17ec54f9e1bb6f78704c/68747470733a2f2f706f7365722e707567782e6f72672f6d696c6164726168696d692f747769747465722d626f742f646f776e6c6f616473)](https://packagist.org/packages/miladrahimi/twitter-bot)[![License](https://camo.githubusercontent.com/2562029717c07287910fca7e8b012e0d6fce6327ebeee4cd78eb56c181eea2c4/68747470733a2f2f706f7365722e707567782e6f72672f6d696c6164726168696d692f747769747465722d626f742f6c6963656e7365)](https://packagist.org/packages/miladrahimi/twitter-bot)

Twitter Bot
===========

[](#twitter-bot)

This package is a basic Twitter bot that handles authentication, HTTP requests and responses, and other primitive requirements. It doesn't focus on any specific Twitter API. Instead, it provides a tool that makes calling Twitter APIs much simpler.

Installation
------------

[](#installation)

Install [Composer](https://getcomposer.org) and run the following command in your project's root directory:

```
composer require miladrahimi/twitter-bot "1.*"
```

Documentation
-------------

[](#documentation)

### Getting Started

[](#getting-started)

The snippet below demonstrates how to create an instance of Twitter Bot. It requires your consumer key (API Key) and consumer secret (API Key Secret). You can get them from your Twitter Developer Portal.

```
use MiladRahimi\TwitterBot\V1\TwitterBot;

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

### Public Endpoints

[](#public-endpoints)

Public endpoints work without authentication and user tokens. You can call public endpoints like the following sample. This sample shows how to call the search API. It searches for tweets that contain the given hashtag.

```
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
}
```

### Authenticated Endpoints

[](#authenticated-endpoints)

Authenticated endpoints work only with authenticated user tokens. A user token includes a user token and a user token secret.

If you want to call Twitter APIs on behalf of yourself, you can get your token and token secret from your Twitter Developer Portal. If you need to call Twitter APIs on behalf of other users, you must authorize them before, as mentioned later in this documentation.

This sample shows how to call the update status API and tweet the given text.

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

### JSON APIs

[](#json-apis)

The example below demonstrates how to call APIs with JSON body (like the direct message API). This sample shows how to call sending direct message API and send the given message to the given user.

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

### Uploading Media

[](#uploading-media)

You might need to upload media. For example, if you want to tweet a photo, you need to upload it first. The following example illustrates how to upload a photo and tweet it.

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

### Authorize Users (Login with Twitter)

[](#authorize-users-login-with-twitter)

This section explains how to authorize users to get their tokens and token secrets. It's an implementation of "Login with Twitter" indeed.

#### Request for Token and Redirection Link

[](#request-for-token-and-redirection-link)

First, you must request a user token and confirm your callback URL implicitly (Consider setting your callback URL in your Twitter Developer Portal). Then you can redirect the user to the Twitter website to approve your request (token).

```
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
}
```

#### Twitter Callback

[](#twitter-callback)

When a user approves your request (token) on the Twitter website, Twitter redirects him to your callback URL. In the callback URL, you can verify the token. Then you can use the token for calling Twitter APIs on behalf of the user.

```
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, ...
```

### Timeout

[](#timeout)

In default, the HTTP (cURL) timeout is 10 seconds. You can set your desired timeout (in seconds) like the following example.

```
use MiladRahimi\TwitterBot\V1\TwitterBot;

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

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

License
-------

[](#license)

PhpRouter is initially created by [Milad Rahimi](https://miladrahimi.com)and released under the [MIT License](http://opensource.org/licenses/mit-license.php).

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~111 days

Recently: every ~167 days

Total

7

Last Release

1354d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/921274b8bb29236d8f94f6c83100a5f751f6394c4c49741e1b92bfbccac0911b?d=identicon)[miladrahimi](/maintainers/miladrahimi)

---

Top Contributors

[![miladrahimi](https://avatars.githubusercontent.com/u/6689295?v=4)](https://github.com/miladrahimi "miladrahimi (13 commits)")

---

Tags

oauthtwittertwitter-apitwitter-bottwitterTwitter APITwitter oAuthtwitter bottwitter auth

### Embed Badge

![Health badge](/badges/miladrahimi-twitter-bot/health.svg)

```
[![Health](https://phpackages.com/badges/miladrahimi-twitter-bot/health.svg)](https://phpackages.com/packages/miladrahimi-twitter-bot)
```

###  Alternatives

[abraham/twitteroauth

The most popular PHP library for use with the Twitter OAuth REST API.

4.3k16.6M135](/packages/abraham-twitteroauth)[hwi/oauth-bundle

Support for authenticating users using both OAuth1.0a and OAuth2 in Symfony.

2.4k22.3M81](/packages/hwi-oauth-bundle)[league/oauth1-client

OAuth 1.0 Client Library

996110.3M119](/packages/league-oauth1-client)[hybridauth/hybridauth

PHP Social Authentication Library

3.4k9.0M108](/packages/hybridauth-hybridauth)[socialconnect/auth

Social Connect Auth Component

568893.3k5](/packages/socialconnect-auth)[mageplaza/magento-2-social-login

Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps

1841.2M5](/packages/mageplaza-magento-2-social-login)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
