This post is also available in: Indonesian [1]
Today I check this home page and my Twitter [2]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.
http://twitter.com/statuses/user_timeline/[user name].rss
just calling function fetch_feed in WordPress (which uses SimplePie [3]) 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 [4]. To know what to do, especially relating to the OAuth and APIs of Twitter. If you need short, lets we shorten it.
- We need to register our application (or web) that will be used to access Twitter, to register click here [5]. 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 isCallback URL: http://www.hikaruyuuki.com/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).
- 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.
- Download PHP code for OAuth and TwitterOAuth here [6].
- Upload to your web via FTP or File Manager, put those, for example in root directory.
- For the PHP code, because I use WordPress, you may need to adjust when implemented in another framework:
<?php require_once './twitteroauth.php'; $twitteroauth = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET); $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(); $rss->set_raw_data($user_timeline); $rss->init(); $rss->handle_content_type(); 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); endif; ?> <ul> <?php if ($maxitems == 0) echo '<li>No items.</li>'; else // Loop through each feed item and display each item as a hyperlink. foreach ( $rss_items as $item ) : ?> <li> <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');?> </time> <h3 title="<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>"> <?php echo processLinks($item->get_title()); ?> </h3> </li> <?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 [7]. Good luck!