Last Updated on April 28, 2021 by Amit

Post Tweet using Twitter API

Do you want to post tweets from your website to your Twitter account?
If yes, then read on!
This post is for you.

In this post I will show you how to post a tweet using Twitter API for PHP. We will use a live demo here to explain the process in detail.
If you want to see the live demo first , then Visit this URL and log in with your Twitter account to see how it works.

What you need to get started?

Before we get started, here are a few requirements that you must meet in order to implement Tweeting from your php website

  • A live web server with PHP 5.4 or above
  • Domain with https certificate
  • Twitter API app.
  • Twitter Auth for PHP

1) Your webserver must have installed PHP version 5.4 or above.

2) Your domain must be SSL enabled.
All API calls from your website to twitter server must be using a secure URL scheme.
If you don’t have an SSL certificate , you can buy one or get a free SSL certificate for your domain from Cloudflare .

3) Create an application on developer.twitter.com for your project.
You will need this application to log twitter users to your website.

To be able to create your first app the twitter developer platform, first you will need to apply for a developer account. Create your developer account on Twitter and send it for review. Once its reviewed you can then create your first app.
3) Download twitter oauth on your server. You need twitter oauth to integrate twitter on your PHP website. You can download the popular twitter Auth library from Here .

What’s next?

We will first implement the Twitter login so that we can log Twitter users to our website.

Download the zip source code from This link and upload it to your web directory.
You can use any Online ftp ediors to upload and unzip the zip archive on your server.

After unzipping the source code file, you need to modify only one single file index.php .
Open the file in your editor and change App key , App secret and callback URL with your values.

You will find API key andsecret on the dashboard of your Twitterper application.
The callback URL is the URL of your site where Twitter users will come after successful authorization on Twitter. You need to add this URL to the settings of your app . If you don’t know the exact location of this URL, just find where the callback.php is located in your Twitter login code.

To verify everything is working perfectly fine, just vist the index.php project file in your browser. This will now ask you to login with your Twitter.

Congratulations : you have successfully implemented Log in with Twitter for your php website. Now we have finished our first step. The next step is to Post a tweet from website .

Post tweet using Twitter PHP API

To post a tweet using Twitter API , we use POST statuses/update method provided by Twitter.
To be able to send tweet using Twitter PHP API, you just need to create a new file in the same directory where your Twitter login source code is located.

Create a new file named post_tweet.php and put the following php code in that file :


require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', 'Your_consumer_key');
define('CONSUMER_SECRET', 'your_consumer_secret');
define('OAUTH_CALLBACK', 'https://example.com/demo/twitter/login/callback.php');
if (!isset($_SESSION['access_token'])) {

$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$user = $connection->get("account/verify_credentials", ['include_email' => 'true']);
// $user1 = $connection->get("https://api.twitter.com/1.1/account/verify_credentials.json", ['include_email' => true]);

$connection->post('statuses/update', array('status' =>'Hello world!'));


We use statuses/update’, array(‘status’ =>’Hello world!’) method to post tweet from website to Twitter account.

With the php code above, anyone who is logged in to Twitter with your application will be able to post a tweet direct from your website to their Twitter account.
The php code above in the post_tweet.php post a short text tweet “Hello world!” to Twitter.

I hope this article was helpful! Thank you so much reading!

Related : Twitter Login for PHP websites with demo and source code .

How useful was this post?

Click on a star to rate it!

Average rating 1 / 5. Vote count: 1

No votes so far! Be the first to rate this post.