(07) 3040 6690

Get Your Latest Tweet with PHP

By: , On Sunday, May 23rd, 2010

The following code uses the SimpleXML extension so it requires PHP5. What it does is search through the XML output of search results for a given user name on Twitter. The version of the code below is set up to bypass Tweets beginning with “@” indicating an @ reply message. This behavior should be simple enough to change if so desired, it should also be easy enough to tweak it to exclude RT: style retweets as well.

<?php
//Get Latest Tweet
function latest_tweet($username,$tweetnumber){
$url = "http://search.twitter.com/search.atom?q=from:$username&amp;rpp=10";
$xml = simplexml_load_file($url);
$tweettitle = $xml->entry[$tweetnumber]->title;
$mytweet = $xml->entry[$tweetnumber]->content;
$firstChar = substr($tweettitle, 0, 1);
//Exclude @ replies
if($firstChar == "@"){
//If this tweet is an @ reply move on to the previous one
while ($firstChar == "@"){
$tweetnumber++;
$tweettitle = $xml->entry[$tweetnumber]->title;
$mytweet = $xml->entry[$tweetnumber]->content;
$firstChar = substr($tweettitle, 0, 1);
if($firstChar != "@"){
//If the previous tweet is not an @ reply output it
return $mytweet;
}
}
}else{
//If first tweet is not an @ reply output it
return $mytweet;
}
}
//End Get Latest Tweet

//output
echo latest_tweet('wardelldesign', 0);
?>

Get Your Last Tweet with PHP [download]


Fatal error: Call to undefined function adsensem_ad() in /home/content/14/4543214/html/wordpress/wp-content/themes/wardelldesign/single.php on line 129