Post to Twitter using OAuth

I have about 30 websites that communicate with Twitter in one way or another and, until a few months ago, I did so via the API that they retired on September 1st. Previous means of sending tweets to Twitter no longer work. The only method of integrating applications now is via OAuth – a far more robust and secure protocol. What I’m providing below is basic instructions on how to set up a basic application on Twitter using OAuth to post a tweet from within your PHP application.

Note: The updated version of this code (that includes sending up to four images with your message is here.

If you’re interested in the nitty-gritty of OAuth (not required to implement what I’ll show you below) you can read about it at or at .

The Twitter OAuth Class

Originally developed by Abraham Williams , TwitterOAuth handles the generation, registration, and authentication of OAuth requests to interface with Twitter. For the purpose of this tutorial, and thanks to the comprehensive class, it’s not really necessary to understand the numerous complexities of OAuth, keys, tokens and so on… but you will have to download and use the (free) library . We will only need two files – “OAuth.php” and “twitterOAuth.php”.

Unzip the files and upload to your server (perhaps into a directory called oauth). Throughout the implementation of this whole thing, watch case-sensitivity. The class has at times changes certain letters from upper-case to lower-case meaning that it’ll break the application. There may be inconsistencies on this page.

Step 1: Log into

Log into using the credentials associated with the Twitter account you’ll be using.

Step 2: Fill out the application details.

Click the “settings” tab and you’re presented with a screen requiring various details. You’re required to fill out a name for the application (this will be the name that’ll appear under each tweet.. Sent from ApplicationName. You must provide a description for the application and a website associated with the account. Callback URL should be left blank. Sending a single tweet, a user won’t directly interact via a browser. Fill in the form, fill out the Captcha and agree to the terms and conditions.

Step 3: Application Type

On the same screen as the Application Details (under the settings tab), you must select the option for “Read and Write“. Your giving the application permission to write to your Twitter timeline. A common error is leaving the application type as ‘Read only’. If this is the case, nothing will be sent to Twitter.

Note: Read-only removes the ability to post tweets or add/remove friends, while Read & Write allows complete access. Read, Write and Access direct messages would give the application further access to the direct messages of a user.

Again, Callback URL should be left blank. If we were building a web-based service, this is the URL that Twitter would send the user (with their access tokens) after they authorize your application.

Click the button at the bottom of the screen – “Update this Twitter application’s settings” – otherwise nothing will save!

Step 4: Application Type

Now, and only now, go back to the “Details” tab and scroll down to the bottom of the screen. You’ll see a button that says, “Recreate my access token” under the heading of ‘Your access token’. Click it: this will generate the necessary Access tokens.

Step 5: Copy your consumer keys and access tokens

If you now select the “OAuth tool” tab you’ll be presented with a screen that has your keys and tokens populated. Copy these keys into the PHP script.

Step 6: Copy your keys and tokens into the PHP code

Insert the keys and tokens from step 4 into the PHP function provided below.

The Code

How is it used?

The $message variable in the PHP code is sent to Twitter.

I have used this code for several months and it’s always worked flawlessly. It’s intended to be used in your own applications so it’s not overly robust if you’re looking at a genuine tool for other people to interact with.

In coming weeks I’ve got a lot of code scheduled – including one handy snippet that will allow you to automatically post an RSS feed to Twitter. Another to post images. Stay tuned.

Common Errors

Here are some common errors I’ve encountered from people thus far.

  1. Requires PHP5 (check with your host if you get lots of errors generated on the screen).
  2. Under the ‘settings’ tab, you must select ‘Read and Write‘ (this is the most common error).
  3. Twitter requires text sent to them to be UTF-8 encoded. Read more about UTF-8 encoding with PHP here .
  4. Make sure you copy the keys and tokens exactly as they’re presented to you.
  5. Server should have CURL libraries installed. Check with your host if you get errors.
  6. It’s best to download the PHP code rather than trying to copy it from the post. Many people have copied with various errors.
  7. From the ‘Details’ tab, if you click “Recreate my access tokens”, you will have to copy the new tokens into the PHP code. There’s generally no need to recreate them after you have generated them the first time.
  8. Probably most importantly is to check CaSe-SensiTivity.


At the time of updating this post, I’m about to embark on a three-month project that’ll severely limit my ability to provide assistance. I’ll do the best I can. The best way of getting in touch is via Skype; my screen name is “martinkhoury”. Make sure you let me know why you’re making the request.

Shortt URL for this post:


  1. iliyas

    Hi, thanx for the post. I have followed all the steps but when i run it ,it doesn’t show anything on page. I don’t know why? help needed.

  2. Pingback: Automate WordPress Posts to Twitter (with hastags, truncation and a short URL)

  3. Nice post!
    Please guide me,
    If I want to show my application icon at the bottom of my post along with
    (12:45 AM – 31 Jan 13 · Details) via php.
    I use same API for twitting.

  4. Marina

    I’ve struggling days now to put a automated message on Twitter and it won’t work. Did some coding of my own to work with twitteroauth.php and OAuth.php and it always redirected me to the applications page (where you press Allow).
    Now that I am trying again just to test it with your php code above and twitteroauth.php. OAuth.php as they are, it gives me this:
    response: {“request”:”\/1\/statuses\/update.json”,”error”:”Could not authenticate with OAuth.”}

    Any idea what’s wrong?
    Thank you in advance!

  5. Marina

    thank you very much for your guidance and the Common Errors paragraph.
    It was very very very helpful. I struggled for days to post a comment on twitter and your php code and guidance helped me the most.
    Thank you!

  6. NC

    […] at first, the Twitter API can be wrestled into submission without much effort. More than anything, this tutorial makes the process considerably more understandable, especially if only making an app for one user

    I want konw how to put link also with this tweet?

  7. Matteo Scandolo

    Hi everybody,

    for who’s trying to use this code now, is needed a small change in twitteroauth.php line 21.

    public $host = “”;

    have to became

    public $host = “”;

    because of the new twitter api.

    Thanks for sharing!

  8. question: in the example file — after changing the keys/tokens you say to change this

    // Full path to twitterOAuth.php (change OAuth to your own path)

    in the twitter dev area is this app a GET request and if so what do you put for request URI and guest query. secondly does this “own path” part above refer to one of those link and if so which one. if not what?

  9. Pingback: Setting up the StudioPress Genesis in Tweets Plugin

  10. Pingback: Post to a Facebook Page with PHP

  11. rahul

    thanks bro…
    This code is working fine…
    i am trying to post on twitter for last 2 day but i am unable to post..
    but i find solution by usin your code..
    for those who are using this code but not getting the output…
    try to change Very IMP
    twitteroauth.php line 21.
    public $host =

  12. Pingback: Retrieve Your Twitter Favorites (Likes) and Display in WordPress with Shortcode | Internoetics

Leave a Reply

Your email address will not be published. Required fields are marked *