You might have wondered as to how a blog or a web-page displays you twitter image when you leave a comment there ? Well there is no rocket science involved in it and I planned why not create a small class for you through which you can show off little extra of your twitter love to the visitors.
We are going to use cURL , a PHP library for this purpose. By cURL we make request to the twitter and get the user details. Getting user detail does not require any authorization, so this is going to be pretty trivial for us. I have explained a major part of this in my another post called “Getting data from a URL using php cURL” so this is going to be enhanced version of that very post. So gear up and lets get started !!
<?php /* * Please do not remove author's information. * Created by Sachin Khosla * URL : http://www.digimantra.com * Date : August 17,2009 * */ class twitterImage { var $user=''; var $image=''; var $displayName=''; var $url=''; var $format='json'; var $requestURL='http://twitter.com/users/show/'; var $imageNotFound=''; //any generic image/avatar. It will display when the twitter user is invalid var $noUser=true; function __construct($user) { $this->user=$user; $this->__init(); } /* * fetches user info from twitter * and populates the related vars */ private function __init() { $data=json_decode($this->get_data($this->requestURL.$this->user.'.'.$this->format)); //gets the data in json format and decodes it if(strlen($data->error)<=0) //check if the twitter profile is valid { $this->image=$data->profile_image_url; $this->displayName=$data->name; $this->url=(strlen($data->url)<=0)?'http://twitter.com/'.$this->user:$data->url; $this->location=$data->location; } else { $this->image=$this->imageNotFound; } } /* creates image tag * @params * passing linked true -- will return an image which will link to the user's url defined on twitter profile * passing display true -- will render the image, else return */ function profile_image($linked=false,$display=false) { $img="<img src='$this->image' border='0' alt='$this->displayName' />"; $linkedImg="<a href='$this->url' rel='nofollow' title='$this->displayName'>$img</a>"; if(!$linked && !$display) //the default case return $img; if($linked && $display) echo $linkedImg; if($linked && !$display) return $linkedImg; if($display && !$linked) echo $img; } /* gets the data from a URL */ private function get_data($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch); curl_close($ch); return $data; } } /* Example on how to use this class*/ //require_once('twitterImage.php') you will require this line if you are saving the above class in a different file //the two lines are enough to display an image off the twitter //$t=new twitterImage('digimantra'); //pass the twitter username //$t->profile_image(true,true); //this will render a linked image /* More info about the params of profile_image function * true,true -- will render a linked image * true,false -- will return a linked image, will not render * false,true -- will render an image without link * false,false (or blank) -- will return an image, will not render */ ?> |
The script is pretty easy to understand and implement. You just have to copy the content and save it in a PHP file. To display an image you have to type only the 2 lines, after you include the above class in your script.
require_once('twitterImage.php') //include the above class $t=new twitterImage('digimantra'); //create instance of the class and pass the username $t->profile_image(true,true); //display a linked image |
You can display an image with and without the link. The image will be linked to the URL provided by user on the twitter’s homepage and if the link is not provided, then it will link to user’s twitter profile by default.
If you think you need some changes or want to add more functionality to the script, let me know. I will be glad to update the script, depending upon the magnitude of the request.
Hope that helps.
Stay Digified !!
Sachin Khosla