Display Twitter User Image on your Blog/Website in 2 Minutes

      

twitter-avatarTwitter API has became a powerful way of getting information about a twitter user and her activities. In my previous tutorial we saw how to create a simple twitter reader in 5 minutes. Let us see how to get the User Image (or Avatar) from Twitter and display it anywhere you want.

So how can I display the Twitter User Image on my Blog in 2 minutes??? All you have to do is to add following IMAGE tag where you want to display Twitter’s User Image and that’s it.

<img src="http://viralpatel.net/blogs/demo/twitter-image.php?user=<TWITTER_USER>" />

In above code, just put the TWITTER_USER name of person whos image you want to display.
For example, for me the above URL will be:

<img src="http://viralpatel.net/blogs/demo/twitter-image.php?user=virp" />

One of the use of this tool is to display user image of people in your blog/website’s comment section. You can have a textbox where users can enter their twitter username and from this you can display their twitter avatar :)

Code behind this simple tool

twitter-logo
We are using cURL to fetch the user details from Twitter. For this we are using the Twitter PHP library that we used in our previous Twitter Client tutorial.

Following is the PHP source code of the Twitter User Image fetcher.

<?php
include("Twitter.class.php");
$twitter = new Twitter();

$username = $_REQUEST["user"];

$json = $twitter->show('json', $username);
$user = json_decode($json);

$image_url = $user->profile_image_url;
header("Location: ".$image_url);
?>

First we use cURL to fetch the user details using twitter API. The response that we get is in JSON format. Hence we use PHP JSON method, json_decode() to convert the JSON into PHP object.


Facebook  Twitter      Stumbleupon  Delicious
  

6 Comments on “Display Twitter User Image on your Blog/Website in 2 Minutes”

  • fhgf wrote on 21 August, 2009, 16:26

    dfdf

  • Bill Bartmann wrote on 5 September, 2009, 20:37

    Cool site, love the info.

  • priyesh das wrote on 8 September, 2009, 7:50

    Thank you for sharing. Great article!

  • images wrote on 24 September, 2009, 19:19

    Excellent work, thank you very much.

  • Sem wrote on 10 November, 2009, 13:16

    Excellent work, Thanx for sharing!

  • Seo wrote on 10 November, 2009, 13:19

    Wowow!!! Cool site.

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 ViralPatel.net. All rights reserved.