Ever since Twitter stop allowing status updates to be posted with some simple php code, my Twitter script has been a big fail whale for everyone. In effort to keep the BrownPHP group happy, here is a little tutorial to post status updates to Twitter with PHP via OAuth.
Link to the BrownPHP Twitter Status Update using OAuth.
Open Authentication Standard Protocol
Now I am not going to get into all the details of “OAuth” or the Open Authentication protocol standard. You can learn more about OAuth and all of its goodness at the OAuth website.
Twitter Authentication Flow Overview
Below is the Flow Overview listed in their documentation that is actually quite helpful to understand the process of authenticating yourself with Twitter.
- Build TwitterOAuth object using client credentials.
- Request temporary credentials from Twitter.
- Build authorize URL for Twitter.
- Redirect user to authorize URL.
- User authorizes access and returns from Twitter.
- Rebuild TwitterOAuth object with client credentials and temporary credentials.
- Get token credentials from Twitter.
- Rebuild TwitterOAuth object with client credentials and token credentials.
- Query Twitter API.
See! Nothing to it!
Pre-Requisites
Once you have completed the Pre-Requisites. Most of the work is actually done.
Folder/File Review
In the twitteroauth code that you have downloaded (and hopefully uploaded to your web server by now) this is the structure that you should see. Included are two folders; images and twitteroauth. Images contains two png files that say, “Sign in with Twitter” that you are free to use with your applications. The twitteroauth folder contains the files that enable the Twitter black magic.
OAuth.php contains many classes used for the OAuth standard. As mentioned above, the goal of this article isn’t to explain OAuth, so just take my word and do not remove this file. The twitteroauth.php file contains the TwitterOAuth class that includes all the methods that you will need for this to work.
config.php is where you will put your consumer key and consumer secret that you should have now after your registered your application with Twitter. Also in this file you will update the OAUTH_CALLBACK define with the url that you want users to be redirected to after they return from Twitter.
callback.php should be the file that you have your config.php file configured to return from Twitter with. This file builds the TwitterOAuth object after the user returns from Twitter.
clearsessions.php does exactly says. It clears the sessions (this will remove the authentication tokens) that have been created and redirects the user to connect.php. connect.php is the start of the example. It basically only has a button (lighter.png/darker.png) to “Sign In with Twitter” that links to redirect.php.
redirect.php is where everything gets started. This file builds the temporary credentials and sends them over to Twitter for authentication. Twitter should then redirect home to the callback.php and then that will redirect to index.php which displays all the information of the user that just logged in. This is nice for showing off all the info that you can pull directly after authenticating with Twitter without having to send any separate requests.
DOCUMENTATION, html.inc, LICENSE, README and test.php are all helpful to skim over, but not really needed for this post.

Recent Comments