How To Send A Tweet On Twitter With PHP

Are you looking for an API integration which sends a tweet on twitter? By using twitter REST API you don’t need to log into your twitter account and post a tweet. You can even post a tweet offline. In this article, we show you how to send a tweet on twitter with PHP.

How To Send A Tweet

For our application, we are using a TwitterOAuth library. This is a popular library for working with twitter REST API.

To install this library you should have composer installed on your system. You additinally need to enable cURL extension on your server.

Register Twitter Application

Our goal is to post tweets on your twitter account without login to twitter. All this task should perform offline. In such scenario, we need some kind of identity token so that we can post a tweet to the desired account.

This is where we need to register an application on twitter and get the API keys from it. These API keys are acts like your identity for your twitter account. To create application, go to Twitter Apps and follow the below steps.

  • Click the button ‘Create New 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 it in safe place.
  • Under the same tab, you will see the section ‘Your Access Token’. Click on the button ‘Create Access Token’.
  • At this step, copy your Access Token and Access Token Secret. Keep these details safe.

Installation Of A TwitterOAuth Library

We are ready with the API keys for our twitter account. Next, we need to install the TwitterOAuth library.

Open the command prompt in your project root directory and run the below command.

composer require abraham/twitteroauth

After installing the library, create a PHP file post-tweets.php and add the below code in it.

post-tweets.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 you have replaced all placeholders with the actual keys.

How to Send a Tweet on Twitter

Twitter has provided RESTful APIs for the developers. To use their APIs we need to send a request to the endpoint URL with required parameters. To post a tweet, Twitter requires posting a text to the endpoint TWITTER_API_URL/statuses/update.

Having said that, let’s write a piece of code for posting a tweet.

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

Twitter allows 140 characters for a single tweet. So make sure you are limiting your text up to 140 characters. If a text is exceeded, then you will get error response like ‘Status is over 140 characters.’

Our final code is as follows.

post-tweets.php

<?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'; //text for your tweet.
$post_tweets = $connection->post("statuses/update", ["status" => $status]);
?>

We hope you understand how to send a tweet on twitter with PHP. Please share your thoughts in the comment section below.

Related Articles

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

1 thought 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. Required fields are marked *