Home     Blog

Send Tweets to your Twitter Account via PHP

Posting status messages to a Twitter account via PHP is quite useful in many scenarios. Like automatically tweeting your blog’s latest content, tweeting list of facts, tips or quotes at regular intervals, tweeting daily news, etc. This article’s intention is to show how you can send a tweet to your Twitter account via PHP programming.

Required APIs, Tokens and Files

We will require the following things in order to tweet via PHP.

  1. An app created via your Twitter account with Read/Write permission.
  2. Consumer Key, Consumer Secret, Access Token, Access Token Secret
  3. OAuth API (i.e. a file called OAuth.php)
  4. PHP Library to support OAuth for Twitter’s REST API (i.e. a file called twitteroauth.php)

Let’s get started by following the below steps carefully.

Step 1: Go to the Create New App screen and login with your Twitter account.

Step 2: On the Create an application screen, enter the required details like Name, Description, and Website for your new Twitter app. Callback URL isn’t necessary and you may leave it blank. Accept the Developer Rules of the Road agreement. Enter the captcha and click Create your Twitter application.

Create-an-application-screen-at-Twitter

Step 3: If your Twitter app is created successfully, you will see the app screen. On this screen under the OAuth Settings section, copy+paste the Consumer Key and Consumer Secret somewhere for future reference.

Clicking the Create my access token will be of no use at the moment, since the Access Level is still Read-Only. We need it to be with Read-Write permissions.

Step 4: Go to the Settings tab. Under the Application Type section, select Read and Write or Read, Write and Access direct messages if your application requires access to Direct Messages too. Click the Update this Twitter application’s settings button.

Twitter-app-Read-and-Write-Permissions

Step 5: Now, go back to the Details tab. You should see Access Level changed to Read and Write under the OAuth Settings section.

Under the Your access token section, click Create my access token button. If successful, you will see newly generated Access Token and Access Token Secret. Copy both the keys for future reference.

Step 6: Download the OAuth.php and twitteroauth.php files and place them in a directory on your server (say under domainname.com/mytwitterbot).

Step 7: Create a new file called index.php and add the following code to it.

<?php
 
// Include twitteroauth
require_once('twitteroauth.php');
 
// Set keys
$consumerKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$consumerSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$accessToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$accessTokenSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
 
// Create object
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
 
// Set status message
$tweetMessage = 'This is a tweet to my Twitter account via PHP.';
 
// Check for 140 characters
if(strlen($tweetMessage) <= 140)
{
    // Post the status message
    $tweet->post('statuses/update', array('status' => $tweetMessage));
}
 
?>

What the code does: This code includes the twitteroauth.php file which already includes the OAuth.php file. Define variables for Consumer Key, Consumer Secret, Access Token, Access Token Secret and copy the respective keys. Create an object of the class TwitterOAuth by passing the keys as parameters. Define a variable which holds the actual status message to be tweeted to your Twitter account. Before posting, it’s safer to check the length of the status message to be below 140 characters – else, the code may show an error. Call the post function as shown with your status message.

Place index.php too in the same directory, i.e. domainname.com/mytwitterbot.

Now, open the URL in your browser. In our case, it would be like http://domainname.com/mytwitterbot. In few seconds, you should see a tweet update in your Twitter account with the set status message.

The status message can be populated from an array, XML file, plain text file, database, or from another API.

Automate Tweets in PHP via CRON
To schedule an auto-tweeting functionality at regular intervals, you can add the above created index.php file to your CRON jobs. This way, the tweet will automatically go to your Twitter account at set intervals.

Did you face any problem while implementing the above method? Please share your experience in the comments below.

VN:F [1.9.22_1171]
Rating: 7.0/10 (6 votes cast)
Send Tweets to your Twitter Account via PHP , 7.0 out of 10 based on 6 ratings
Follow Abdulqader Kapadia on
  • Utpal

    Hello, I have made the same thing what you have said but when running the file then nothing happening and also the tweet message is not appearing on my twitter account. Please help me how can i solve the issue.

    Thanks,

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • WillSpencer

      Utpal, try recreating your Access Token and Access Token Secret after setting your Access Level changed to Read and Write.

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      • Utpal

        thanks for your reply. It’s working fine now. thanks again

        VA:F [1.9.22_1171]
        Rating: 0.0/5 (0 votes cast)
  • Utpal

    Hello, I have one another question regarding direct message on twitter. Do you have any example code of how can i send direct message to multiple twitter account.

    Thanks,

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • Abdulqader Kapadia

    Mattias, I didn’t really understand your question. Can you please elaborate?

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • http://www.facebook.com/jahrvos Abrahim Vos

    Great script sofar, thanks, works like charm. Next step I don’t really understand. How to get the status message into a variable.
    And further: how to include an image. I tried it with adding the URL of the image in the message but that doesn’t seem to work…

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • Ivan van Oosterhout

    I get this error???

    Fatal error: Cannot redeclare class OAuthException in /home/deb00000/domains/website.com/public_html/testtwit/OAuth.php on line 8

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • Abdulqader Kapadia

      This could be the result of declaring/including “twitteroauth.php” multiple times. I recommend you follow the above steps from scratch in a fresh directory.

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • Jason

    Worked exactly as you said it would first time. THANKS!!! for this great walk-though

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • http://www.madebyword.com/ Abdulqader Kapadia

      Jason, thank you. We are glad it helped you.

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • Mushtaq

    thanks a million man you rock man really

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • Mushtaq

    brilliant guys my best wishes with u guys keep rocking

    may god help u guys to help others

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • Neeraj

    Well done guys

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • JDR

    Getting an error:

    Warning: require_once(twitteroauth.php) [function.require-once]: failed to open stream: No such file or directory in /homepages/20/d98661292/htdocs/mro/twitterbot/index.php on line 4

    Fatal error: require_once() [function.require]: Failed opening required ‘twitteroauth.php’ (include_path=’.:/usr/lib/php5′) in /homepages/20/d98661292/htdocs/mro/twitterbot/index.php on line 4

    I’m a total php noob so forgive me if this is something obvious

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • jdr

      so sorry realized i had the php files in the wrong sub dir my bad

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • http://www.facebook.com/ahmed.5.hisham Ahmed Hisham

    Many thanks for the tutorial…

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  • Meenu

    Hey Very nicely explained.. but ca u please tell me how to send tweets via xml file

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • http://www.madebyword.com/ Abdulqader Kapadia

      You will need to implement XML logic to read from an XML file, where it says “Set status message” in the code.

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • Nirmal Sharma

    Hi, when I implemented this code, it worked perfectly. But after some time, this code stopped tweeting.. Please tell me why so?

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    • http://www.madebyword.com/ Abdulqader Kapadia

      I’m not sure, but it seems like a problem with your CRON settings or server.

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  • divyesh

    how to post image(photo) in twitter using above your code..

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)