Now twitter is playing a major roll to our day. Many of us is using twitter. Now lets play something with it. Today i will use JSON to play with twitter LOL.
Lets play with our statuses. If you are using twitter, then your status url is http://twitter.com/status/user_timeline/YOUR_USERNAME.json. So if your twitter id is tareq_cse then the url will be http://twitter.com/status/user_timeline/tareq_cse.json. Now i will add a extra parameter here and this is count. This url shows the last 20 status update of yours. So if i need only 5 update then? Then we will add a count variable there. So the url will be http://twitter.com/status/user_timeline/tareq_cse.json?count=5
Now come to the php code.
<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true); //getting the file content
$decode = json_decode($json, true); //getting the file content as array
echo "<pre>";
print_r($decode);
echo "</pre>";
?>
Now run the code and see. You will get output like this
Array
(
[0] => Array
(
1 => is digging wordpress
[in_reply_to_user_id] =>
[user] => Array
(
[notifications] =>
[time_zone] => Dhaka
[description] => A blogger, microblogger and learner
[following] =>
[utc_offset] => 21600
...............
...............
[profile_text_color] => 666666
[location] => Rajshahi, Bangladesh
[id] => 15842888
[profile_background_image_url] => http://static.twitter.com/images/themes/theme9/bg.gif
[profile_link_color] => 2FC2EF
)
[favorited] =>
[in_reply_to_screen_name] =>
[truncated] =>
[created_at] => Sun May 03 20:56:00 +0000 2009
[id] => 1689697302
[in_reply_to_status_id] =>
1 => TwitterFox
)
)
Now get some usefull data from this. If we need the last 10 twitter update write the code
<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true);
$decode = json_decode($json, true);
echo "<pre>";
$count = count($decode); //counting the number of status
for($i=0;$i<$count;$i++){
echo $decode[$i]1."<br>";
}
echo "</pre>";
?>
Lets pull some userdata
<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true);
$decode = json_decode($json, true);
echo "<img src=\"".$decode[0][user][profile_image_url]."\"</img><br>"; //getting the profile image
echo "Name: ".$decode[0][user][name]."<br>"; //getting the username
echo "Web: ".$decode[0][user][url]."<br>"; //getting the web site address
echo "Location: ".$decode[0][user][location]."<br>"; //user location
echo "Updates: ".$decode[0][user][statuses_count]."<br>"; //number of updates
echo "Follower: ".$decode[0][user][followers_count]."<br>"; //number of followers
echo "Following: ".$decode[0][user][friends_count]."<br>"; // following
echo "Description: ".$decode[0][user][description]."<br>"; //user description
?>
Following this method, you can pull any data by analyzing the first output