Sunday, June 17, 2012

Parsing xml user timeline & json search Twitter di WordPress

Twitter memberikan dokumentasi API yang sangat komprehensif untuk dikembangkan oleh para developer dalam membuat suatu aplikasi. Twitter REST API Resources tersedia secara gratis untuk para pengembang. Tutorial berikut adalah 2 contoh hasil api Twitter dalam bentuk PHP yang digunakan di CMS WordPress.

Parsing xml status user_timeline Twitter
Kode ini untuk menampilkan status twitter dari user.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php 
function twitter_statuses_user_timeline() {
$twitter_screen_name = 'jevuska'; //edit username twitter
$tweet_count =2; //edit jumlah tweet
$loaddataxml = 'https://api.twitter.com/1/statuses/user_timeline.xml';
$loaddataxml .= '?include_entities=true';
$loaddataxml .= '&include_rts=true';
$loaddataxml .= '&screen_name='.$twitter_screen_name;
$loaddataxml .= '&count='.$tweet_count;
return apply_filters('twitter_statuses_user_timeline',$loaddataxml);
}
 
function load_twitter_statuses_user_timeline(){
$dataxml = simplexml_load_file(twitter_statuses_user_timeline());
echo "<img src=".$dataxml->status->user->profile_image_url.">";
foreach($dataxml->status as $twitter_user_status) {
echo "status : ".$twitter_user_status->text."<br>";
echo "Time : ".$twitter_user_status->created_at."<br>";
}
return true;
}
load_twitter_statuses_user_timeline();
?>

Parsing json search status Twitter
Kode berikut untuk menampilkan tweet berdasarkan query yang sudah ditentukan.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php 
function twitter_statuses_search() {
$twitter_query = 'blue angels'; //query yang dicari
$tweet_count =5; //jumlah tweet
$loaddatajson = 'http://search.twitter.com/search.json';
$loaddatajson .= '?q='.urlencode($twitter_query);
$loaddatajson .= '&rpp='.$tweet_count;
$loaddatajson .= '&include_entities=true';
$loaddatajson .= '&result_type=mixed';
return apply_filters('twitter_statuses_search',$loaddatajson);
}
 
function load_twitter_search(){
$twitter_response_status = file_get_contents(twitter_statuses_search() );
$twitter_json_status = json_decode($twitter_response_status);
foreach((array)$twitter_json_status->results as $twitter_search_results) {
echo "Text : ".$twitter_search_results->text."<br>";
echo "Created at : ".$twitter_search_results->created_at."<br>";
echo "From User : ".$twitter_search_results->from_user."<br>";
echo "Profile Image : <img src=".$twitter_search_results->profile_image_url."><br>";
 
}
return true;
}
load_twitter_search();
?
com/docs/api


Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 komentar

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
Posts RSSComments RSSBack to top
© 2013Bangun blogger Let's be smart. ∙ Designed by jhony Bangun
Released under Creative Commons 3.0 CC BY-NC 3.0