Api Twitter

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.

<?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.

<?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();
?>

Dokumentasi Api Twitter secara keseluruhan dapat dilihat di https://dev.twitter.com/docs/api

Published by

Jevuska

Rachmanuddin Chair Yahya a.k.a Jevuska is the founder of Jevuska.Com, a qualified web about offering medical articles, blogging, tips, and tutorial of WordPress. Having written for Jevuska since 2007.

Code is my cookies. ~ Jev
Before you read or leave a comment, please take a couple minutes to read our Copyright & Disclaimer notice.

Tinggalkan Komentar