Back in 2009, I wrote a post on how to write a simple PHP script to call on the Twitter API and update your status. Despite its popularity, the information hasn’t been relevant in some time. (Things certainly have changed since then!) The Twitter API has changed a lot over the years, and it’s not so simple that you can get a newbie up and running with a few lines of code.
The mandatory usage of OAuth tokens, rather than a simple username and password combination, for API requests has greatly strengthened account security, but it’s one of the prime hurdles complicating the process. More recently, XML support was removed in favor of JSON, URL structures changed to include an API version, and authentication is now required for every request.
Fortunately, you don’t have to deal with the little details. You can use a library that does the heavy lifting for you, rather than reinventing the wheel. Sure, there are resources to learn how to do it the hard way, but I assume that you want a quicker solution if you’re reading this.
Step 1: Download tmhOAuth
Download the tmhOAuth library from GitHub. This package will handle interactions with the Twitter API once you include it from your script. (It requires at least PHP 5.1.2 and the cURL extension.)
Step 2: Include the Library in Your Project
First, drop the required files into your project folder. You could simply move the entire download in and rename it, or if you just want the essentials, the required files are tmhOAuth.php
and cacert.pem
.
Now, you need to include the files in your PHP script. All you need is an include line at the top.
include 'tmhOAuth/tmhOAuth.php';
Step 3: Get Your Tokens
We need to make a short detour, now. In order to give your script permission to update your status, you need to generate some authorization tokens for it. Head over to dev.twitter.com/apps and log in to your Twitter account.
Click the big “Create a new application” button and fill in the required fields on the resulting screen. All you need is an application name to identify your script, a brief description, and the URL it will reside at.
Once you agree to the terms of service and submit the form, select the Settings tab on the resulting screen. Change the access type from Read Only to Read and Write before saving the settings.
Flipping back to the Details tab, you now need to scroll down to the OAuth settings section. You will need the consumer key and consumer secret, as well as the access token and access secret from the following section.
After the include lines, you need to instantiate the tmhOAuth class with the tokens, like so:
$tmhOAuth = new tmhOAuth(array( 'consumer_key' => 'THE_CONSUMER_KEY', 'consumer_secret' => 'THE_CONSUMER_SECRET', 'token' => 'THE_ACCESS_TOKEN', 'secret' => 'THE_ACCESS_TOKEN_SECRET', ));
This readies tmhOAuth to interact with the API through your account. (Allowing other Twitter users to log in and grant permission is beyond the scope of this tutorial. But Tuts+ has you covered if you want a more advanced tutorial.)
Step 4: Sending the Tweet
To send a tweet, all you have to do now is call the request
method and pass the required arguments.
$response = $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update'), array( 'status' => 'Test message. Lorem ipsum.' ));
You can, of course, replace the message with anything you want (providing it’s under 140 characters) and even use a dynamic variable instead of a static string.
Step 5: Verify Success
Now let’s see if Twitter accepted the request and successfully updated your status.
if ($response != 200) { //Do something if the request was unsuccessful echo 'There was an error posting the message.'; }
Conclusion
That wasn’t too difficult, was it? While it’s a simple example, it should help you get started in the right direction. The tmhOAuth library can handle just about any Twitter API request you may need to make, and the GitHub repository includes plenty of examples to browse through.