Twitter's popularity can be attributed to a number of factors. One of those factors is the Twitter's open API. From desktop clients to web-based management tools for brands, Twitter's open API gives every developer the ability to develop cool and useful applications that enhance the Twitter user experience and extend Twitter's utility.
One of the nicest things about Twitter's API is that, like Twitter itself, it's pretty darn simple. In this post, we'll discuss how you can get started developing for Twitter using PHP and a handy PHP class.
Requirements
Before you begin developing, you'll need a few things:
- A Twitter account.
- A development server.
- Knowledge of a programming language.
Given Twitter's popularity, it's not surprising that there are many client libraries available that eliminate the need for developers to reinvent the wheel. From ActionScript to Ruby, The Twitter API Wiki lists libraries for a variety of programming languages.
For the purposes of this post, we'll be working with PHP and the easy-to-use PHP Twitter class. To use this class, your server will need PHP 5.2 and the lib_curl PHP module installed.
Getting Started
Once you've downloaded the PHP Twitter class, upload it to a directory your server. Since I like to place classes in their own directory, we'll assume that the PHP Twitter PHP file has been uploaded to a classes folder in your HTTP root.
When creating a PHP script that interacts with Twitter, the first thing you'll need to do is create an instance of the PHP Twitter class. Here's the code for that:
require_once('classes/class.twitter.php');
$t = new Twitter;
$t->username = 'twitterusername';
$t->password = 'twitterpassword';
Now that you're officially instantiated, the Twitter API is your oyster. Let's look at some of the things you'll probably want to do.
Retrieving Tweets
The following code will retrieve and echo your own tweets:
$tweets = $t->userTimeline();
foreach($data as $tweet) {
echo $tweet->text . "<br />";
}
Sample output:
Working with the Twitter API! This is amazing!
Not all that interested in yourself? Find out what your friends are up to by retrieving your friends' tweets:
$tweets = $t->friendsTimeline();
foreach($data as $tweet) {
echo $tweet->user->screen_name . " tweeted: " . $tweet->text . "<br />";
}
Sample output:
someuser tweeted: Going to the beach! anotheruser tweeted: Working late (again).
Each of the methods used above (userTimeline and friendsTimeline) allows you to pass in a number of parameters. Be sure to check out the Twitter API documentation for information on these.
Sending Tweets
Listening is important on Twitter but tweeting is probably more fun. Using the Twitter API, it's easy to send tweets:
$tweet = $t->update("If a tre falls in the forest and...");
The above code will post a tweet stating "If a tre falls in the forest and..." to the timeline of the authenticated user.
Of course, there's a typo in the above tweet. Fortunately, there's a method for deleting tweets. So let's go ahead and delete our tweet before anybody sees our typo. First, we have to know the ID of the tweet we just sent. The update method returns the ID of every new tweet that has been posted so we can retrieve the ID of our typo tweet with the following:
$tweet_id = $tweet->id;
Now that we have the ID of the tweet to delete, we can get rid of it using the destroy method, which is called as follows with Twitter PHP:
$tweet_delete = $tweet->deleteStatus($tweet_id);
Putting It All Together
To put it all together, let's write some simple code that will take the most recent tweet from the authenticated user's friends' timeline and retweet it.
$tweet = $t->friendsTimeline('', '', '', '1');
$retweet_string = "RT @" . $tweet->user->screen_name . " " . $tweet->text;
$retweet = $t->update($retweet_string);
Obviously, it's always good to do some error checking. Here, for instance, you may want to know beforehand whether your tweet is going to exceed 140 characters and deal with it if it is.
Taking It to the Next Level
As you can see from these simple examples, it's quite easy to retrieve and send tweets with Twitter's API using the PHP Twitter class. Tasks such sending direct messages, retrieving information about users, pulling followers and accessing Twitter's search functionality are equally easy to perform using other API methods Twitter provides. These generally work in the same fashion as those methods discussed here.
For a complete list of the API methods Twitter offers and more detailed information about the API, be sure to check out the Twitter API Wiki. You'll find that the most important methods are accessible through the PHP Twitter class.
By combining these straightforward methods in creative ways, building fun and useful Twitter applications can be done in very short order.
1 comments:
It's amazing how over 75% of Tweets now come from third-party apps and not from the actual Twitter site. I wonder if this hurts Twitter's business valuation. What are your thoughts?
Post a Comment