- Impact
- 89
This is the start of a script I made following a tutorial I wrote (currently pending) on pixel2life's publishing system about grabbing your twitter status.
I may decide to add more features in time as there is a large scope to add more things however this is what I done quickly for my own site:
-----------------------
If you want to post about the code elsewhere like on your own blog please link to this topic and http://mediascratch.com, that is all I ask. You are not required to place credit if you use the code, it is entirely free and of course you are welcome to modify it providing initial credits remain with my name and website.
Enjoy
Matt
I may decide to add more features in time as there is a large scope to add more things however this is what I done quickly for my own site:
-----------------------
If you want to post about the code elsewhere like on your own blog please link to this topic and http://mediascratch.com, that is all I ask. You are not required to place credit if you use the code, it is entirely free and of course you are welcome to modify it providing initial credits remain with my name and website.
PHP:
<?php
/* This class is compatible with >= PHP 5 only due to the use of:
SimpleXML: http://uk2.php.net/simplexml
Used to parse XML feeds
SimpleXML is enabled by default. If it appears not to be, contact your host.
cURL is also required for this script to operate. http://uk.php.net/curl
The Twitter API documentation used to create this class can be found:
http://groups.google.com/group/twitter-development-talk/web/api-documentation
Written by Matt Jewell
http://mediascratch.com
*/
class twitterAPI {
public $twitter_base = 'http://twitter.com/statuses/';
private $twitter_username = 'your username';
private $twitter_password = 'your password';
public function fetch_latest_status() {
/* user_timeline
Returns the 20 most recent statuses posted in the last 24 hours from the authenticating user. It's also possible to request another user's timeline via the id parameter below.
Parameters used: id
count
*/
$buffer = file_get_contents($this -> twitter_base . 'user_timeline/' . $this -> twitter_username . '.xml?count=1');
// Grab the contents of the XML file and store it in the variable.
$xml = new SimpleXMLElement($buffer);
// Creating a new XML string for use with the SimpleXML extension.
$status_item = $xml -> status;
// Creating a new variable using SimpleXML with "status" as the Node.
return $status_item -> text;
// Grab the current status as the Element.
}
function update_status($message){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this -> twitter_base . 'update.xml?status=' . stripslashes(urlencode($message)));
// The variable could also be substituded by the use of CURLOPT_POSTFIELDS
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Enables us to return the result as a string instead of outputting to browser.
curl_setopt($curl, CURLOPT_POST, 1);
// From the Twitter API docs: "Request must be a POST". CURLOPT_POST ensures this./
$username = $this -> twitter_username;
$pass = $this -> twitter_password;
curl_setopt($curl, CURLOPT_USERPWD, "$username:$pass");
// Authenticates the user (you) in the post request.
$exec = curl_exec($curl);
// Execute the cURL request.
// echo curl_error($curl);
// Used to debug cURL.
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Return the HTTP headers so we can confirm the request.
// A list of what the status codes are and mean can be found here:
// http://www.seoconsultants.com/tools/headers.asp#code-200
curl_close($curl);
// Terminate the cURL session
return ($httpcode == 200) ? 'Updated!' : 'Error!';
/* Ternary operator equivelant to:
IF httpcode equals 200
Return done
ELSE
Return error
*/
}
}
?>
Enjoy
Matt







