Created
May 3, 2010 18:16
-
-
Save jc1arke/388399 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class GetMyTweets | |
| { | |
| private $username = null; | |
| private $password = null; | |
| /** | |
| * Overloaded constructor used to set the username and password for the class | |
| */ | |
| public function __construct( $_username, $_password ) | |
| { | |
| $this -> username = $_username; | |
| $this -> password = $_password; | |
| } | |
| /** | |
| * Public functino that gets called by external programs to get and display the tweets according to the count, or it will use the default of 20 :) | |
| */ | |
| public function displayTweets( $count = 20 ) | |
| { | |
| $tweets = $this -> getTweets( $count ); | |
| } | |
| /** | |
| * Private function used to get the tweets and display them | |
| */ | |
| private function getTweets( $count = 20 ) | |
| { | |
| // Initialize counter to zero | |
| $counter = 0; | |
| // Get the twitter feed via the fetchExternFeed function | |
| $twitter_feed = $this -> fetchExternalFeed( "http://twitter.com/statuses/user_timeline.xml", $this -> username, $this -> password ); | |
| // Failsafe check to make certain that a actual XML object was returned | |
| // Also, I am using the new way of using if-statements, so don't get confused ;) | |
| if( $twitter_feed ) : | |
| // Remember the foreach from a previous post? | |
| // As you can see, I am also using the same shorthand for the foreach as with the if-statement | |
| // Here I am just iterating over all the statuses that were returned from the twitter feed | |
| foreach( $twitter_feed -> status as $status ) : | |
| // Check whether the counter is still less than the allowed count | |
| if( $counter < $count ) : | |
| // Iterate over the keys of the status (there are quite a few entries per status, such as GeoData, time, application used, etc etc. Read the | |
| // Twitter Wiki for more information about this | |
| foreach( $status as $key => $value ) : | |
| // Evaluate the keys and find the one's that you want to interact with | |
| // I left a couple of the other key's in here so that you can experiment a bit and see what it does ;) | |
| switch( $key ) : | |
| case "created_at" : break; // Date and time of the tweet in GMT format | |
| case "id" : echo "Tweet ID : " . $value . "<br />"; | |
| break; | |
| case "text" : echo "Tweet : " . $value . "<br />"; | |
| break; | |
| case "source" : break; // The application's name that was used to create the tweet | |
| case "in_reply_to_status_id" : break; // The tweet could have been in reply to another tweet, and this would contain that tweets' ID. | |
| // You can use this to get that specific tweet ;) | |
| case "in_reply_to_user_id" : break; // The id of the original poster that this is a reply on | |
| case "favorited" : break; // If the tweet has been favorited | |
| case "in_reply_to_screen_name" : break; // The twitter username of who the reply is to | |
| case "user" : break; // This username | |
| default : break; | |
| endswitch; | |
| endforeach; | |
| echo "<hr />"; | |
| // Increment counter | |
| $counter++; | |
| endif; | |
| endforeach; | |
| else : | |
| echo "Could not retrieve twitter feed at this time"; | |
| endif; | |
| } | |
| /** | |
| * This function uses the cURL library to fetch a remote server's source via a specific URL/feed and authenticates with the username and password provided | |
| * and returns the results as a XML document object that PHP can interact with | |
| */ | |
| private function fetchExternalFeed($feed, $user, $pass) | |
| { | |
| $ch = curl_init( $feed ); | |
| curl_setopt($ch, CURLOPT_HEADER, true ); | |
| curl_setopt($ch, CURLOPT_TIMEOUT, 30 ); | |
| curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass ); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false ); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false ); | |
| $result = curl_exec( $ch ); | |
| $data = strstr( $result, "<?" ); | |
| $xml = new SimpleXmlElement( $data ); | |
| return $xml; | |
| } | |
| } | |
| $myTweets = new GetMyTweets( "twitter_username", "twitter_password" ); | |
| $myTweets -> displayTweets( 20 ); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment