December 31, 2013 7:15 am

How to post tweet on Twitter with PHP

Twitter is one of the most famous micro blogging social site where you can explain your thoughts in 140 letters and today I am going to write an article on how to post a tweet on Twitter with PHP and this is a very simple procedure we already post an article on Twitter oAuth login and this will be supplement of that I hope you guys took benefit from it specially for fresh web Developers can learn more.


In this tutorial files included as below:

Contains two folders called oAuth and images with PHP files.

– twitteroauth.php //Functions to call for actions.
– OAuth.php // Twitter OAUTH library
– callback.php //Call back page create permanent credentials
– config.php // Configuration
– destroysessions.php // Erase all old sessions
– index.php // Main index file show data

To post tweets on Twitter you have to create an App click here

PHP Code to operate all the actions.


Contains your CONSUMER_KEY and CONSUMER_SECRET which you got when register application.

You have to modify this file and add your API credentials and call back url.


Contains PHP code to show sign in with Twitter image and redirect to twitter for authenticate and on redirect show text box to update tweet.

When you loggedin it will store your access_token and oauth_token in session and use it till you destroy it.

if(strlen($status)>=140) used to check length of your message because twitter accept only 140 letters in a single tweet.

$url = $connection->getAuthorizeURL($token); Get authentication url and redirect to the twitter for permission.


Contain PHP code to verify authentication and create sessions of token and redirect to index.php file where you can see text box to add tweet and post it to your twitter.


Contain PHP code used to destroy session and redirect to index.php for fresh login.


We are looking forward for your feedback and comments, also added a demo for this and source code to download hope you like it.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:
  • fjvalencian

    Hi, i am trying to run this code in my server, is this correct?

    Thanks for your help.

  • gomathy

    Thanks for coding its working! i want to get user name, id and mail id through this login. How can i get user details?

  • gomathy

    Thanks for coding its working! i want to get user name, id and mail id through this login. How can i get user details?

  • Sean

    I am getting Warning: Cannot modify header information – headers already sent by (output started at /home/seanbfep/public_html/testing/twitter/index.php:24) in /home/seanbfep/public_html/testing/twitter/index.php on line 52, idk what i am doing wrong

  • Usama

    I want to know If i have saved the auth_code and secret value in DB . Now after few days i triger an event to post a tweet for the user for which i have already auth tokens.

    Best Regards

    • Mark Bagnall

      I’ve got it working, if you want me to send it to you

  • Mark Bagnall

    When sharing URL, its not turning into a link on the tweet