For those that are unfamiliar with the Twitter API or are looking to quickly fetch tweets from a user’s profile, this class will help.
In PHP, all you do is include the class in your document and construct it with a Twitter username (with some options for extra tweaking).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <?php class UserTweets { var $username = ''; var $page_content = ''; var $encoded_content = array(); var $results = array(); var $user_info = array(); var $result_count = 0; var $connected = false; function __construct($username, $options = array()) { $this->username = $username; $request_options = array( 'since_id', 'max_id', 'count', 'page' ); $option_set = array(); $option_string = 'screen_name='.urlencode($username); if(!empty($options)) { foreach($request_options as $req) { if(isset($options[$req])) { $option_set[$req] = $options[$req]; $option_string .= '&'.$req.'='.urlencode($options[$req]); } } } $url = 'http://api.twitter.com/1/statuses/user_timeline.json?'.$option_string; $connection = @curl_init(); if($connection) { curl_setopt($connection, CURLOPT_URL, $url); curl_setopt($connection, CURLOPT_HEADER, false); curl_setopt($connection, CURLOPT_RETURNTRANSFER, true); curl_setopt($connection, CURLOPT_CONNECTTIMEOUT, 30); $this->page_content = curl_exec($connection); curl_close($connection); $this->connected = true; } else { $connection = @file_get_contents($url); if($connection) { $this->page_content = $connection; $this->connected = true; } } if($this->connected) { $this->results = json_decode($this->page_content, true); $this->result_count = count($this->results); if($this->result_count > 0) { $this->user_info = $this->results[0]['user']; foreach($this->results as &$result) { unset($result['user']); } } } } } ?> |
Usage
To fetch a user’s tweets with default settings (simple method).
1 | $tweets = new UserTweets('stevedecoded'); |
Setting options with names and values as defined in the Twitter API Wiki. This example returns 5 results posted on or before the update with ID of 12402600172.
1 2 3 4 5 6 | $options = array( 'max_id' => 12402600172, 'count' => 5 ); $tweets = new UserTweets('stevedecoded', $options); |
Displaying tweets in an ordered list.
1 2 3 4 5 6 7 8 9 10 | if($tweets->result_count > 0) { echo '<h1>Tweets</h1>'; echo '<ol>'; foreach($tweets->results as $tweet) { echo '<li>'.$tweet['text'].'</li>'; } echo '</ol>'; } |
…And user info. This data is stripped out of the results
array and added into its own named user_info
to reduce bloat.
1 2 3 4 5 6 7 8 9 10 | if($tweets->result_count > 0) { echo '<h1>User Info</h1>'; echo '<ol>'; foreach($tweets->user_info as $name => $info) { echo '<li>'.$name.' => '.$info.'</li>'; } echo '</ol>'; } |
This script will work on server setups with either the cURL library or allow_url_fopen turned on, taking preference over the faster and more secure cURL. If none are in use, most of the object properties will be empty (like results
and page_content
) and it will handle itself properly without printing errors to the page.
Hope this class will make it a bit easier for developers to start using the great Twitter API in their own mashups.
Comments are welcome as always.
Have fun 🙂
Thanks for posting this. I am trying to establish a more effective twitter approach than javascript calls for each web page in a site and this script is very useful for me to build on. Other libraries are so much over kill – this script is just right!
Thanks Matt, glad you found it useful.