categories: development / tutorials /
Awhile ago I wrote a light Twitter PHP class that could grab and update your status. I also threw in two methods for Tiny and Tr.im url shortners. Check it out.
// DEFINE TWITTER CLASS AND UPDATE STATUS WITH OUTPUT RETURNING HTTP CODE
$run = new twitterClass();
echo $run->UpdateStatus('my message');
// TWITTER CLASS
class twitterClass {
// SET GLOBAL VARIABLES
private $twitter_username = '';
private $twitter_password = '';
private $trim_username = '';
private $trim_password = '';
// CHECK WHERE THE REQUESTING URL IS COMING FROM
public function CheckReferer(){
$url = parse_url($_SERVER["HTTP_REFERER"]);
return $url;
}
// GRAB THE RETURNED CODE FROM HTTP REQUEST AND REFERENCE DEFINITION
public function HTTP_Code($code){
switch ($code) {
case 200:
$return = "Updated!";
break;
case 301:
$return = "Moved Permanetly!";
break;
case 302:
$return = "Found!";
break;
case 304:
$return = "Not Modified!";
break;
case 307:
$return = "Temporary Redirect!";
break;
case 400:
$return = "Bad Request!";
break;
case 401:
$return = "Unauthorized!";
break;
case 403:
$return = "Forbidden!";
break;
case 404:
$return = "Not Found!";
break;
case 410:
$return = "Gone!";
break;
case 500:
$return = "Internal Server Error!";
break;
case 403:
$return = "Not Implemented!";
break;
default:
$return = "Bad Return Code! Code: $http_code_return. ";
}
return $return;
}
// REPLACE WITH Tiny URL
public function TinyURL($url){
$return = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
return $return;
}
// REPLACE WITH Tr.im URL
public function TrimURL($url){
$xml = file_get_contents("http://api.tr.im/api/trim_url.xml?url=".$url."&newtrim=yes&username=".$this->trim_username."&password=".$this->trim_password);
$return = new SimpleXMLElement($xml);
return $return->url;
}
// GET LAST STATUS UPDATE
public function LatestStatus() {
$buffer = file_get_contents("http://twitter.com/statuses/user_timeline/".$this->twitter_username.".xml?count=1");
$xml = new SimpleXMLElement($buffer);
$return = $xml->status;
return $return->text;
}
// UPDATE STATUS
public function UpdateStatus($message){
$message = urlencode($message);
$username = $this->twitter_username;
$password = $this->twitter_password;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://twitter.com/statuses/update.xml?status=$message");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$exec = curl_exec($curl);
$http_code_return = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->HTTP_Code($http_code_return);
curl_close($curl);
return $return;
}
}
7 comments
-
Posted: Apr 22, 2010
Awesome, I had something similar but this one is a lot cleaner.
Does this one filter out for @replys? -
Posted: Apr 26, 2010
Shouldn't filter anything out. I can create a simple script to filter @replys if you're looking for that kind of thing.
-
Posted: Jun 30, 2010
Looked at this code yesterday and realized its extremely outdated. To give you some context, I wrote this a year ago. Twitter has, since then, moved on to using oAuth to authenticate so this script will pretty much be deprecated in a little bit of time.
I'll write a new class for anyone wanting to do simple oAuth (especially since the current examples in PHP aren't very well written)
Hold on tight and I'll get out a few new tutorials soon for this.
Cheers -
Posted: Jul 5, 2010
Can you help me to implement this on my web site pleeeease?
Thanks in advance! -
Posted: Jul 22, 2010
Nice! Going to try this over the weekend.
-
Posted: Aug 20, 2010
Hey man.
Rather than rewriting this class every year or whenever twitter does a major update to their API why not refactor, and decouple the class to make future releases more flexible and maintainable. You could use the DAO pattern as an example to manage your twitter data source/connection. A business logic abstraction layer/object could then handle basic CRUD operations such as UpdateStatus, getLatestStatus and whatever else you decide to add in later versions. -
Posted: Aug 20, 2010
Hey man.
Rather than rewriting this class every year or whenever twitter does a major update to their API why not refactor, and decouple the class to make future releases more flexible and maintainable. You could use the DAO pattern as an example to manage your twitter data source/connection. A business logic abstraction layer/object could then handle basic CRUD operations such as UpdateStatus, getLatestStatus and whatever else you decide to add in later versions.