Using Twitter API is simple and there is a ton of documentation available. But sometimes people get lost in lot of text and it is hard to find the right thing fast.
I will show you some snippets for getting info from Twitter and embed it in your web page. Basically, I will just show the actual code for getting the job done, so there is no styling or formatting in this snippets.
Table of Contents
Twitter search
Common thing that people want to use on a page is implementing a search box from which a user can search different things on Twitter. So here is how to do it:
[code lang=”php”]
// search term
$query = "jquery";
// we are using CURL to access the API
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://search.twitter.com/search.atom?q=" . urlencode($query) . "&rpp=10&result_type=popular");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
header(‘Content-Type: application/xml; charset=UTF-8’);
// just echoing plain XML result
echo $result;
[/code]
You can see the demo for above code here.
Get user’s followers on Twitter
Other common thing is to get all the followers of specified user. The code below will get all my followers and display its avatars. which are links to their Twitter profile:
[code lang=”php”]
<?php
$username = ‘codefoest’;
$follower_url = "http://api.twitter.com/1/statuses/followers/" . $username . ".xml";
$friends = curl_init();
curl_setopt($friends, CURLOPT_URL, $follower_url);
curl_setopt($friends, CURLOPT_RETURNTRANSFER, TRUE);
$twiFriends = curl_exec($friends);
$response = new SimpleXMLElement($twiFriends);
foreach($response->user as $friends){
$thumb = $friends->profile_image_url;
$url = $friends->screen_name;
$name = $friends->name;
?>
<a title="<?php echo $name;?>" href="http://www.twitter.com/<?php echo $url;?>"><img class="photo-img" src="<?php echo $thumb?>" border="0" alt="" width="40" /></a>
<?php
}
?>
[/code]
You can see the demo for above code here.
Combining the two snippets
Now I am going to search the Twitter and then display the followers from the first user in the search results:
[code lang=”php”]
<?php
$query = "jquery";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://search.twitter.com/search.atom?q=" . urlencode($query) . "&rpp=1&result_type=popular");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
$response = new SimpleXMLElement($result);
foreach($response->entry as $entry) {
$author_uri = $entry->author->uri;
break;
}
$temp = explode(‘/’, $author_uri[0]);
// results in Array ( [0] => http: [1] => [2] => twitter.com [3] => username )
$author = $temp[3];
$follower_url = "http://api.twitter.com/1/statuses/followers/".$author.".xml";
$friends = curl_init();
curl_setopt($friends, CURLOPT_URL, $follower_url);
curl_setopt($friends, CURLOPT_RETURNTRANSFER, TRUE);
$twiFriends = curl_exec($friends);
$response = new SimpleXMLElement($twiFriends);
foreach($response->user as $friends){
$thumb = $friends->profile_image_url;
$url = $friends->screen_name;
$name = $friends->name;
?>
<a title="<?php echo $name;?>" target="_blank" href="http://www.twitter.com/<?php echo $url;?>"><img class="photo-img" src="<?php echo $thumb?>" border="0" alt="" width="40" /></a>
<?php
}
?>
[/code]
You can see the demo for above code here.
Is user A following user B?
The last snippet is going to show you how to check the friendship status. It just return true or false.
[code lang=”php”]
$user_a = ‘codeforest’;
$user_b = ‘slice_machine’;
$follower_url = "http://api.twitter.com/1/friendships/exists.xml?user_a=".$user_a."&user_b=".$user_b;
$friends = curl_init();
curl_setopt($friends, CURLOPT_URL, $follower_url);
curl_setopt($friends, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($friends);
header(‘Content-Type: application/xml; charset=UTF-8’);
echo $result;
[/code]
You can see the demo for above code here.
This concludes this simple collectin of useful Twitter snippets. I will expand this tutorial later on, adding some advanced functionality that uses Twitter oAuth like updating your status or following somebody. So, stay tuned.
Great. Thank you
Comments are closed.