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.
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 dev.twitter.com
Log into dev.twitter.com 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.
/* Insert your keys/tokens */
$consumerKey = 'insert-your-consumer-key';
$consumerSecret = 'insert-your-consumer-secret';
$OAuthToken = 'insert-your-OAuthToken';
$OAuthSecret = 'insert-your-OAuthSecret';
/* Full path to twitterOAuth.php (change OAuth to your own path) */
/* create new instance */
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);
/* Your Message */
$message = "This is a test message.";
/* Send tweet */
$tweet->post('statuses/update', array('status' => "$message"));
How is it used?
$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.
Here are some common errors I’ve encountered from people thus far.
- Requires PHP5 (check with your host if you get lots of errors generated on the screen).
- Under the ‘settings’ tab, you must select ‘Read and Write‘ (this is the most common error).
- Twitter requires text sent to them to be UTF-8 encoded. Read more about UTF-8 encoding with PHP here .
- Make sure you copy the keys and tokens exactly as they’re presented to you.
- Server should have CURL libraries installed. Check with your host if you get errors.
- It’s best to download the PHP code rather than trying to copy it from the post. Many people have copied with various errors.
- 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.
- 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: