- Hikaru {Aka} Yuuki のブログ - http://www.hikaruyuuki.com -
Ambil Status (User Timeline) dari Twitter
Posted By Adikara Putra (Hikaru Yuuki) On 5日 11月 2010年 @ 20:57 In Pemrograman,Website & Blog | No Comments
Baru saja mengecek ke home [2] page web ini ternyata status Twitter [3]tidak ditampilkan lagi. Hal ini dikarenakan Twitter tidak lagi memberikan ijin untuk mengakses langsung RSS dari status saya (user timeline), lagipula karena saya juga mengeset protect my tweets untuk tweets baru. Sebelumnya dengan mudah saya tinggal mem-fetch RSS dari alamat URL
http://twitter.com/statuses/user_timeline/[user name].rss
menggunakan fungsi fetch_feed pada WordPress (yang menggunakan SimplePie [4]) dan otomatis di-parsing sehingga mudah untuk mereproduksi hasil RSS user timeline tersebut. Tapi berhubung sekarang Twitter menggunakan OAuth untuk otentikasinya, jadi saya harus mengotak-atik sedikit kode untuk mem-fetch user timeline Twitter. Sebelumnya untuk memulai ada baiknya membaca dari Get Started API Twitter [5]. Untuk mengetahui apa yang harus dilakukan terutama yang berhubungan dengan OAuth dan API dari Twitter. Ya, kita persingkat saja.
Application Website: [alamat yang digunakan untuk mengakses atau tempat kita menaruh aplikasi untuk mengakses Twitter]Application Type: Browser (karena saya menggunakan web bukan desktop client)Callback URL: [alamat yang digunakan untuk mengakses Twitter]kalau punya saya Callback URL: http://www.hikaruyuuki.com/Default Access type: Read & WriteBila sudah selesai dan akan menyimpan, kita harus menekan tombol I Accept untuk menerima TOS (Terms of Service) dari Twitter.
Kemudian cari My Access Token di sidebar sebelah kanan dan klik sehingga muncul halaman OAuth 1.0a access token (bersifat rahasia dan jangan sampai bocor) Catat setingan berikut ini (misal copy-paste di Notepad) :
Empat settingan di atas inilah yang digunakan sebagai otentikasi OAuth di Twitter lewat aplikasi/web kita.
<?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; ?>
Selain untuk mengambil status, kita juga bisa mengupdate status, melihat direct message, mentions, friends, dll. Untuk lebih lengkapnya bisa dilihat dan dicoba-coba sendiri melalui Twurl Console [8]. Selamat mencoba!
Article printed from Hikaru {Aka} Yuuki のブログ: http://www.hikaruyuuki.com
URL to article: http://www.hikaruyuuki.com/blog/ambil-status-user-timeline-dari-twitter.html
URLs in this post:
[1] Your Ad Here: http://www.adbrite.com/mb/commerce/purchase_form.php?opid=1784967&afsid=1
[2] home: http://www.hikaruyuuki.com/
[3] Twitter : http://twitter.com/AdikaraPutra
[4] SimplePie: http://simplepie.org/
[5] Get Started API Twitter: http://dev.twitter.com/start
[6] alamat disini: http://dev.twitter.com/apps/new
[7] dari sini: https://github.com/abraham/twitteroauth
[8] Twurl Console: http://dev.twitter.com/console
[9] Ubah Permalink WordPress Tanpa Kehilangan Traffic Blog: http://www.hikaruyuuki.com/blog/ubah-permalink-tanpa-kehilangan-pagerank.html
[10] Ubah Manual Driver Letter dari Mounted Device (Partisi, Media, dll) Lewat Registry Windows: http://www.hikaruyuuki.com/blog/ubah-manual-driver-letter-dari-mounted-device-partisi-media-dll-lewat-registry-windows.html
Click here to print.
Copyright © 2011 Hikaru {Aka} Yuuki のブログ. All rights reserved.