Get Your Latest Tweet with PHP
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&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]







July 19th, 2010 at 10:51 am
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:
July 31st, 2010 at 3:52 am
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
May 11th, 2011 at 5:55 am
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:
May 25th, 2011 at 2:29 pm
Sorry about that I had some trouble with the WYSIWYG editor converting characters again, the code above should work now
June 6th, 2011 at 8:55 am
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?
June 6th, 2011 at 10:28 am
Disregard my comment, it was an error on my part.
June 7th, 2011 at 10:32 am
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:
June 7th, 2011 at 11:18 am
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
June 7th, 2011 at 5:30 pm
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:
June 7th, 2011 at 9:03 pm
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.
http://snipt.net/ryancooper/latest-tweet