Code Snippet
Get Latest Twitter Status
<?php
function getTwitterStatus($userid){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=1";
$xml = simplexml_load_file($url) or die("could not connect");
foreach($xml->status as $status){
$text = $status->text;
}
echo $text;
}
//my user id kenrick1991
getTwitterStatus("kenrick1991");
?>
All I get is a ‘Could not Connect’ Error. Code doesn’t seem work.
The code works. There are a couple reasons you might get a “Could no Connect” that come to mind:
1. Your installation of PHP does not support simple_xml
2. Your server is not able to connect to an external site.
The first scenario is probably more likely.
can only receive the latter can not appear more
Another reason for the code to not work properly is that the twitter account used to retrieve the status update may be ‘locked’ (private accounts).
You should set it up to public status update ;)
Thanks for this snippets works perfect for me!
I would advise against using the XML format, at least for now. It appears that retweets are not included in the XML and JSON feeds. That wouldn’t be so bad, except that it doesn’t just skip a retweet – it returns a blank tweet!
To get around this I use the RSS format. That comes with its own unique challenges and a different XML structure but at least it returns those retweets.
More info:
http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline
How would you change it to show the last X tweets instead of just 1?
I implemented the above code but the links in my tweets are not active even though they are active on my twitter page. Have any thoughts?
I know you nearly lost your shit trying to publish this code, but it says everything you need to know in the remember section right by this comment form.
Thanks for the code, works perfect!
i used code it works for me but i cannot get the date of tweets. Can you help me to get the tweet date.
Not sure if this has been posted, I found a slight error with your foreach loop.
if you set the ?count=5 (or something other than one) you will only echo the 5th most recent tweet you’ve made. If that is what you’re after, great, but if you want to show the 5 most recent tweets, you’ll need to put the “echo $text;” within the foreach:
in the $url, change ?count=1 to ?count=5 or something other than one
Well, it worked perfectly at first, then after refreshing the page, it gave a could not connect to server error. Wonder why it worked initially?
awesome, thank you Chris.
Both versions works great for me, now to just stylize it and put it on my homepage for my users. Thanks!
i got it every thing just working fine :) thx Chris
It works intermittently for me. It will be displaying everything fine and then, a moment later, it stops and I get the “Could not Connect” error.
Any ideas?
I was having issues getting it to work and I realized that
$text = $status->text;
would need to be
$text .= $status->text;
Otherwise the code will only print one.
Good tricks!
I made some modifications to the script. Hope this help someone! :)
<?php
function getTwitterStatus($userid){
$i = 1;
do {
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$i";
$i++;
$xml = simplexml_load_file($url) or die("could not connect");
$text = utf8_decode($xml->status->text);
} while (empty($text));
$text = ereg_replace("[[:alpha:]]+://[^[:space:]]+[[:alnum:]/]", "\", $text);
echo $text;
}
getTwitterStatus("nicholascamp");
?>
Couldn’t get this to work in mine. I used this url and it worked:
$url = “http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=$userid&count=1″;
i used @Walter code it works for me but i cannot get the date of tweets. Can anyone help me to get the tweet date.
I’m working with a Drupal install, trying to forego using OAuth and the current iteration of the Twitter module, using this code instead. Unfortunately the “Could not connect” error is site crippling for me. It gives me a white screen and completely locks me out of accessing my site, until the error finally just (randomly) ceases and the code is functional again. Is there any way to stop this behavior, or a bit of code that might work “better” for my needs? Any help is appreciated. Thanks!
I changed a little something in Walter’s code because Greek characters showed up like questionmarks for me.
So I replaced utf8_decode($text) with html_entity_decode($text) and now my Greek chars are just fine!
<?php
function getLastXTwitterStatus($userid,$x){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$x";
$xml = simplexml_load_file($url) or die('could not connect');
echo '<ul>';
foreach($xml->status as $status){
$text = twitterify( $status->text );
echo '<li>'.utf8_decode($text).'</li>';
}
echo '</ul>';
}
function twitterify($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
$ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
$ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
return $ret;
}
//my user id kenrick1991
getLastXTwitterStatus('kenrick1991',5);
?>
I have been checking out different way’s to embed twitter in php and this is by far the best and easiest I’ve seen.
Is there a way to embed hashtag’s like through twitter search instead of a user id? Would be great for events!
How do I cache my tweet because my page seem to load slower than normal?
Does anybody know how I can use PHP to grab the StatusesCount from a Twitter account and then return to it to a webpage as a plain text number to format with CSS? I am currently displaying the 5 most recent tweets and below have a link ‘view more tweets’ that links to the twitter account but I am keen to say ‘view more tweets, we have x available to read’. Where x is the count that gets returned through the PHP. Can anybody help, I thought it would be easy to find, but having searched for the past two hours I haven’t been able to track down how to do it. Thanks T
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.