(07) 3040 7074

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]

Loading Facebook Comments ...

10 Responses to “Get Your Latest Tweet with PHP”

  1. David Says:

    Thank you for the great info.

    I am trying o use this PHP code on my site, but it is coming up blank. Is this specific to WordPress?

    Wardell Reply:

    hi David, sorry for the delayed reply this code is not specific to WordPress. do you have error reporting turned on when trying to debug and if so are you receiving any error messages? what version of php are you using? Is it 5.2.14, 5.3 or some other version? if you’re running a version prior to 5.1.2 you’ll have to manually enable simplexml http://www.php.net/manual/en/simplexml.installation.php

  2. avatare Says:

    Sorry it´s not working, either you forgot something or i just made a mistake and am not able ti fix it. But thanks anyway

    Wardell Reply:

    Sorry about that I had some trouble with the WYSIWYG editor converting characters again, the code above should work now

  3. Ryan Cooper Says:

    I keep getting “Trying to get property of non-object” for lines 6 and 7 in the sample above… This just started happening, do you know if something changed perhaps?

  4. Ryan Cooper Says:

    Disregard my comment, it was an error on my part.

  5. Ryan Cooper Says:

    It seems the error i mentioned occurs when the defined user hasn’t posted in several days, could you confirm this? I’ve tested it with several names, if a user has posted lately it pulls the tweet if its been more than 5 days it gives the error?

    Wardell Reply:

    Hi Ryan, the script as it stands actually uses the feed generated by a Twitter search so if its been a while since the users last post it’s likely that no search results are being returned , and the reason why an error is generated. It should be possible to refine the script to use a users timeline instead of a search result. I’ll try and updated it soon but if you wanted to take a crack at it here is a link with info on how to get a users timeline feed http://dev.twitter.com/doc/get/statuses/user_timeline

  6. Ryan Cooper Says:

    This is what i was able to come up with. I was unable to omit the usernames like your previous version but actually like it this way since i was echoing the username before the function anyway. :) Thanks for pointing me in the right direction.

    http://api.twitter.com/1/statuses/user_timeline.atom?screen_name=$username&count=10

    Ryan Cooper Reply:

    Sorry for so many comments but i went back and figured it out. I used the XML feed and switched all the tags accordingly. The ATOM feed used “title” and “content”, while the XML version only used “Text” so i combined/omitted the tweettitle lines, as i don’t think they were needed any longer, but i could be wrong. Here’s a link to the code snippet which i currently have set to public, however let me know when you received it and ill remove it. :D

    http://snipt.net/ryancooper/latest-tweet

© Wardell Design 2014
Entries (RSS) and Comments (RSS).