Get Status (User Timeline) from Twitter

Posted in: My Diary |

This post is also available in: Indonesian

Twitter dengan OAuth

This post is also available in: Indonesian

Today I check this home page and my Twitter status is no longer displayed. This is because Twitter is no longer give permission to access directly the RSS of my status (user timeline), and also because I protect my tweets (for new tweets). Before this, I can fetch easily the status from the RSS URL address such below.[user name].rss

just calling function fetch_feed in WordPress (which uses SimplePie) and you’ll get it automatically parsed, so it is easy to reproduce the result of this RSS users timeline. But since Twitter now is using OAuth for authentication, so we need a little tinkering with code to fetch the Twitter user timeline.

Before we proceed it’s good to start reading from the Get Started API Twitter. To know what to do, especially relating to the OAuth and APIs of Twitter. If you need short, lets we shorten it.

  1. We need to register our application (or web) that will be used to access Twitter, to register click here. Fill out all needed fields (name, description, etc) and you must pay attention more at the following
    • Application Website: [url address you use to access or you put your application to access Twitter]
    • Application Type: Browser (because I use web not desktop client application)
    • Callback URL: [address used to access Twitter] mine is Callback URL:
    • Default Access type: Read & Write

    After you done and saved it, you must click I Accept button  to accept the Twitter’s TOS (Terms of Service).

  2. After it saved, you will see the Application Details page that contains the OAuth Settings. Note the following settings (eg, copy-paste in Notepad)
    • Consumer Key
    • Consumer secret

    Then find My Access Token in right sidebar and click it. After OAuth 1.0a access token page is displayed (information here is confidential so do not let it leak or tell anyone!), note the following settings (eg. copy-paste in Notepad) :

    • Access Token (oauth_token)
    • Access Token Secret (oauth_token_secret)

    These above four settings are needed for authentication using Twitter OAuth through our application/web.

  3. Download PHP code for OAuth and TwitterOAuth here.
  4. Upload to your web via FTP or File Manager, put those, for example in root directory.
  5. For the PHP code, because I use WordPress, you may need to adjust when implemented in another framework:
    require_once './twitteroauth.php';
    $twitteroauth->format = 'rss';
    $user_timeline = $twitteroauth->get('statuses/user_timeline');
    <h2 id="latest-twitter"><?php _e('Latest <span>On</span> Twitter'); ?></h2>
    <?php // Get RSS Feed(s)
    include_once(ABSPATH . WPINC . '/feed.php');
    // Get a SimplePie feed object from the specified feed source.
    $rss = new SimplePie();
    if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly
     // Figure out how many total items there are, but limit it to 5.
     $maxitems = $rss->get_item_quantity(3);
     // Build an array of all the items, starting with element 0 (first element).
     $rss_items = $rss->get_items(0, $maxitems);
     <?php if ($maxitems == 0) echo '<li>No items.</li>';
     // Loop through each feed item and display each item as a hyperlink.
     foreach ( $rss_items as $item ) : ?>
     <time title="<?php echo $item->get_date('c');?>" datetime="<?php echo $item->get_date('c');?>">
     <strong><?php echo $item->get_date('M');?></strong>
     <br />
     <?php echo $item->get_date('j');?>
     <h3 title="<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>">
     <?php echo processLinks($item->get_title()); ?>
    <?php endforeach; ?>

In addition, no only to retrieve the status, we can also update the status, fetch the direct message, mentions, friends, etc. For more details you can see and try those through Twurl Console. Good luck!

Speak Up!

Leave your own comment

Notify me of follow-up comments via e-mail (or subscribe here).



Subscribe Feed







Google Buzz