How do I read and view a Twitter feed from the user?


This might be a big question, but I find the Twitter developer website even more confusing than the Facebook developers site. So after finishing the Facebook API, I'd like to do some more research and find out how the Twitter API works.

What I want to do is make a basic application that could read and display a logged in users Twitter home feed, and publish a Tweet to their profile. But first things first, how do I read and show a users feed? First off I'll have to authenticate a user and my application. Could anyone show me an example on that with code that I could read up on? Then I'll have to read that users feed. This is done by sending a GET request to /statuses/user_timeline.json, but how do I do this in code? How do I receive the response, and lastly how can I display that to a user?

I know Facebook had an example on their webpage which walked you through step by step a process like this (with code), but do Twitter have anything like that? If not, could anyone who has a clue give me a little guide and some code?

Now, if you know/ have the time to respond further, I would like to know how I can publish something to a users stream. I assume it's done by sending a POST request to something, but I haven't read up on that yet. If you know, please feel free to add it to your answer. If not, no stress. Reading the stream is the most important.

So, thanks for taking the time to read through this and hopefully someone knows how to do this/ where to read up on this. Thanks in advance.

  • Aleksander

I use PHP to get a users tweets. I'm presuming it could be done with JS too.

Here is what I use:

function getTweets($userid,$x){
    $url = "$userid.xml?count=$x";
    $xml = simplexml_load_file($url) or die('Could not get tweets');
    echo '<ul>';
    foreach($xml->status as $status){
      echo grabTweetData($status);
    echo '</ul>';

function grabTweetData($status) {
    $id = $status->id;
    $user = $status->screen_name;
    $text = twitterify( $status->text );
    $timestamp = $status->created_at;
    $date = substr($timestamp, 0, 10).', '.substr($timestamp, -4).' at '.substr($timestamp, 11, 5);

    $tweet = '<a href="'.$user.'/status/'.$id.'"><li>';
    $tweet .= '<div class="ttext">'.utf8_decode($text).'</div>';
    $tweet .= '<div class="tdate">Posted on: '.$date.'</div>';
    $tweet .= '</li></a>';
    return $tweet;

function twitterify($ret) {
    //links #tags and @users
    $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=\"\\1\" >@\\1</a>", $ret);
    $ret = preg_replace("/#(\w+)/", "<a href=\"\\1&src=hash\" >#\\1</a>", $ret);
    return $ret;