views:

86

answers:

3

Hi Guys,

I was just wondering, what PHP methods would I use to get the most recent tweet of a twitter user to display in a box on my website?

any suggestions on how i may achieve this would be great.

cheers

+1  A: 

Try a library: http://dev.twitter.com/pages/libraries#php

Or, if those seem like overkill for some reason, load the feed via cURL and simplexml_load_string. Manipulate as needed.

This will show your last tweet:

<?php

$ch = curl_init("http://twitter.com/statuses/user_timeline/{yourtwitterfeed}.rss");
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

$data = simplexml_load_string(curl_exec($ch));
curl_close($ch);

echo $data->channel->item[0]->title;

** EDIT: Ha. Or you can use simplexml_load_file("{url goes here}") instead of all that cURL stuff. I forgot you can do that.

George Mandis
Wow, Brilliant thankyou. never seen that curl library of functions before. thats awesome to know. However i wondered if there was way to do this without installing the curl library. Alternatively, how do i install this library?
Ryan Murphy
If your version of PHP doesn't have cURL setup, there are instructions here:http://www.php.net/manual/en/curl.setup.phpDepending on your setup and experience this can be a bit of a pain.Client-side solutions for Twitter are probably better if all you want to do is a display a single tweet or two from a specific user. I'd look at the link user356981 supplied below, Twitter's own profile widget (http://twitter.com/goodies/widget_profile) or jTweetsAnywhere (http://thomasbillenstein.com/jTweetsAnywhere/).
George Mandis
+1  A: 

There is also a built in simplexml function:

// Parse the raw xml
$xml    =    simplexml_load_file('http://twitter.com/statuses/user_timeline/{yourfeed}.rss');


// Go through each tweet
foreach ($xml->channel->item as $tweet)
{
    echo $tweet->title . '<br />';
}
Kieran Allen
put that in your website and you will block your servers sockets and eventually get blocked by twitter, your website will bei significantly slower and you have an external uncontrollable error source that can kill your whole site.- do error handling, cache the results, think of some logic when and how to fetch. through a user hitting the website is possible but a very bad idea. cronjob? if you fetch it on page generation use some sort of locking to ensure only one request is fetching data and storing it for other requests
Joe Hopfgartner
A: 

http://jquery.malsup.com/twitter/

P.j.Valle