Whether you’re building a social media tool, automating tweeting directly from your PHP application, scheduling tweets, or just experimenting with APIs, Twitter makes it possible through its developer platform.
In this tutorial, you’ll learn how to post tweets and images on Twitter using PHP and TwitterOAuth, a popular PHP library that simplifies working with the Twitter API. We’ll guide you step by step — from setting up your Twitter Developer account to sending your first tweet programmatically.
I am using TwitterOAuth because it is straightforward and well documented.
Prerequisite:
1. Twitter Account
2. Twitter Developer App
3. TwitterOAuth: Twitter API Library
1. How to create a Twitter Developer App
To use the Twitter API library, you need to create a Twitter Developer App. Don’t worry if you haven’t created one yet — we’ll guide you through it.
But before that, make sure you have a Twitter account. If you don’t have one, just sign up — it’s quick and easy!
Step 1: Log in to Twitter Developer Portal
For Twitter App, you’ll need to go to the Twitter Developer Portal. If it takes you to the twitter login page, just login to proceed.

Step 2: Create a Project
You will need to create a project and generate your API Key, API Secret, Access Token, and Access Token Secret. Press the “Create New App” button.

Step 3: Create an Application
Fill out the “Create an application” form with the required details, then click the “Create your Twitter application” button to continue.

Step 4: Set Permissions
Navigate to the “Permissions” tab and set the access level to “Read and Write”. After that, click the “Update Settings” button to save the changes.

Step 5: Generate Access Token
Go to the “Keys and Access Tokens” tab and click the “Create my access token” button. This will generate your access token.

Step 6: Copy API Credentials
From “keys and Access Tokens” tab, you can find your required credentials like API Key, API Secret, Access Token, and Access Token Secret. Do not share it with anyone.

After creating Twitter App, you will get a Consumer Key (API Key), a Consumer Secret (API Secret), an Access Token and an Access Token Secret. These credentials is must to communicate between your app and the API, so do not share them with anyone.
2. Install Abraham/TwitterOAuth in your project
To use this library in your project, you need to install it. The easiest way to install the library is via Composer, but you can also download it manually and place it in your project folder. You can also follow the full installation steps in the official TwitterOAuth documentation.
Install the package via composer, run:
composer require abraham/twitteroauth
Inlcude the TwitterOAuth class in your PHP file.
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
Start making API requests.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");
3. How to Post Tweets and Images on Twitter with PHP
Step 1: Add API Credentials
Add your Twitter API credentials to your project’s configuration file so you can access them easily in your code.
define('CONSUMER_KEY','YOUR_CONSUMER_KEY');
define('CONSUMER_SECRET','YOUR_CONSUMER_SECRET');
define('OAUTH_TOKEN','YOUR_AUTH_TOKEN');
define('OAUTH_SECRET','YOUR_AUTH_TOKEN_SECRET');
define('SCREEN_NAME','YOUR_PROJECT_NAME');
Step 2: Initialize TwitterOAuth
Then, create an instance of the TwitterOAuth class to access Twitter API functions.
require("config.php");
require("vendor/autoload.php");
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get("account/verify_credentials");
Step 3: Post Text Tweet
If you only want to post a tweet to your own Twitter account, use the following API request.
$statues = $connection->post("statuses/update", ["status" => "hello world"]);
Step 4: Post Tweet with Image
Similarly, use the following code if you want to post a tweet with an image.
require("config.php");
require("vendor/autoload.php");
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$media1 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten1.jpg']);
$media2 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten2.jpg']);
$parameters = [
'status' => 'Hello Word with Images!',
'media_ids' => implode(',', [$media1->media_id_string, $media2->media_id_string])
];
$result = $connection->post('statuses/update', $parameters);
Hope this was helpful.
checkout our Best laravel packages to help you extend your project’s functionalities easily.
2 Responses
Thanks man! Wicked helpful, it has been probably 5 years or more since I coded a twitter bot, this helped get me there, hello tweet worked, now I am onto coding an automated / random meme generator by selecting from a directory of pics, and an array of text data, and pairing them together! (I will probably use something like imagemagik to do the trick) Gonna be awesome / wicked simple but effective!
Thank you very much Jephro!