How to Send A Tweet On Twitter with PHP

Do you want to know how to send a Tweet on Twitter with PHP? The Twitter API allows you to post a Tweet programmatically on your Twitter account. In this article, I show you how to send a Tweet on Twitter with Twitter API and PHP.

For the API integration, I’ll use the TwitterOAuth library. This is an open-source library used for Twitter OAuth REST API.

With the help of this library, I’m going to write a code for

  • Post a Tweet on Twitter
  • Post a Tweet along with images on Twitter

Register Twitter Application

While integrating Twitter API, you’re required to create an application on the Twitter Developer Platform. Upon creating an application, you’ll get API keys that are necessary while calling API endpoints.

These API keys act as an identity for your Twitter account. To register an application, go to the Twitter Developer website and follow the below steps.

  • Click the button ‘Create an App’.
  • Fill up Name, Description, Website fields.
  • Accept agreement and click the button ‘Create your Twitter application’.
  • On the next page, click on the tab ‘Keys and Access Tokens’. Under this tab you will find your Consumer Key and Consumer Secret. Copy these details and store them in a safe place.
  • Under the same tab, you will see the section ‘Your Access Token’. Click on the button ‘Create Access Token’.
  • Copy your Access Token and Access Token Secret.
Twitter Credentials

Installation of TwitterOAuth Library

You are ready with the API keys of your Twitter account. Next, install the TwitterOAuth library.

To install this library you should have Composer installed on your system. Also, make sure the cURL extension is enabled on your server.

Open the command prompt in the project root directory and run the command:

composer require abraham/twitteroauth

After installing the library, we have to include the environment of a library and define API keys. Add the below code to your PHP file.

<?php
require_once "vendor/autoload.php";
 
use Abraham\TwitterOAuth\TwitterOAuth;
 
define('CONSUMER_KEY', 'ENTER_YOUR_CONSUMER_KEY');
define('CONSUMER_SECRET', 'ENTER_YOUR_CONSUMER_SECRET');
define('ACCESS_TOKEN', 'ENTER_YOUR_ACCESS_TOKEN');
define('ACCESS_TOKEN_SECRET', 'ENTER_YOUR_ACCESS_TOKEN_SECRET');
 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

Make sure to replace all placeholders with their equivalent values.

Send a Tweet on Twitter with PHP

Twitter has provided RESTful APIs for the developers. In order to use their APIs, you need to send a request to the endpoint URL with the required parameters.

For example, posting a Tweet requires you to hit the TWITTER_API_URL/statuses/update endpoint. Don’t worry about the base URL of an API endpoint. The library will handle it for you.

That being said, the code to send a Tweet will be as follows.

$status = 'This is a test tweet. https://artisansweb.net';
$result = $connection->post("statuses/update", ["status" => $status]);

Here, the library hits the API endpoint in the background and posts a Tweet on your Twitter account.

For some reason if your Tweet is not posting then you can print the error received in a response.

if ($connection->getLastHttpCode() == 200) {
    echo "Your Tweet posted successfully.";
} else {
    echo 'error: ' . $result->errors[0]->message;
}

Our final code is as follows.

<?php
require_once "vendor/autoload.php";
 
use Abraham\TwitterOAuth\TwitterOAuth;
 
define('CONSUMER_KEY', 'ENTER_YOUR_CONSUMER_KEY');
define('CONSUMER_SECRET', 'ENTER_YOUR_CONSUMER_SECRET');
define('ACCESS_TOKEN', 'ENTER_YOUR_ACCESS_TOKEN');
define('ACCESS_TOKEN_SECRET', 'ENTER_YOUR_ACCESS_TOKEN_SECRET');
 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
 
$status = 'This is a test tweet. https://artisansweb.net';
$result = $connection->post("statuses/update", ["status" => $status]);
if ($connection->getLastHttpCode() == 200) {
    echo "Your Tweet posted successfully.";
} else {
    echo 'error: ' . $result->errors[0]->message;
}

Post a Tweet with Media

If you are looking to send images into your Tweet via API, you can do it using the statuses/update endpoint. You first need to upload media via Twitter API which in return will give the media id. This media id needs to be sent as an additional parameter.

Let’s write a code for it. I assume you have a 1.jpg file in the directory.

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$media = $connection->upload('media/upload', ['media' => getcwd(). '/1.jpg']);
$parameters = [
    'status' => 'Testing Media Upload via API',
    'media_ids' => $media->media_id_string
];
$result = $connection->post('statuses/update', $parameters);

if ($connection->getLastHttpCode() == 200) {
    echo "Your Tweet posted successfully.";
} else {
    echo 'error: ' . $result->errors[0]->message;
}

Just in case, if you wish to send more than one image then change the code as follows.

$media1 = $connection->upload('media/upload', ['media' => getcwd(). '/1.jpg']);
$media2 = $connection->upload('media/upload', ['media' => getcwd(). '/2.jpg']);
$parameters = [
    'status' => 'Testing Media Upload via API',
    'media_ids' => implode(',', [$media1->media_id_string, $media2->media_id_string])
];

I hope you understand how to post a Tweet on Twitter with PHP. This tutorial should help you to integrate Twitter API in no time. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

5 thoughts on “How to Send A Tweet On Twitter with PHP

  1. Thanks so much!

    I was looking around for a simple tutorial on how to send tweets. This one put me in the right direction in no time. I had my first test done in just under 15 minutes and it worked instantly.

    Keep up the good work!

Leave a Reply

Your email address will not be published.