How To Display Twitter Profile Picture On My Website

Posted on 2009-07-07
Last Modified: 2012-05-07
Hi Everyone,

I've found interesting code at @

I can display the timeline but i can not display the image (profile image of selected person)

Can someone help?

$name = 'andy_murray';

if( !file_exists("twitter$name.cache.txt") || filemtime("twitter$name.cache.txt") < time() - 300 ) {

    file_put_contents("twitter$name.cache.txt", file_get_contents("$name.rss"));


$items = simplexml_load_string( file_get_contents("$name.rss") )->xpath("/rss/channel/item/title[not(starts-with(.,\"$name: @\"))]/..");

echo '<?xml version="1.0" encoding="UTF-8"?>';


    <h1><a typeof="foaf:OnlineAccount" rel="foaf:accountServiceHomepage" resource="" property="foaf:accountName" href="<?php echo 

$name; ?>/">

<?php echo $name; ?></a></h1>

<div id="myTwitter">


	foreach( $items as $tweet) {

		$diff = Timesince(strtotime($tweet->pubDate));

		$link = $tweet->link;

		$post = preg_replace( '|([^\s])\s+([^\s]+)\s*$|', '$1&nbsp;$2',


						substr($tweet->description, strlen("$name: "))),ENT_QUOTES,'UTF-8'));

		echo "<span style=\"display:none;\">"; 		

		echo date("d/m/Y H:s",strtotime($tweet->pubDate));

		echo "<a href=$link>$post</a>";

		echo "Posted: ".$diff;

		echo "</span>";




<script type="text/javascript">






function Timesince($original) {  

     // array of time period chunks  

     $chunks = array(  

     array(60 * 60 * 24 * 365 , 'year'),  

     array(60 * 60 * 24 * 30 , 'month'),  

     array(60 * 60 * 24 * 7, 'week'),  

     array(60 * 60 * 24 , 'day'),  

     array(60 * 60 , 'hour'),  

     array(60 , 'min'),  

     array(1 , 'sec'),  



     $today = time(); /* Current unix time  */  

     $since = $today - $original;  


     // $j saves performing the count function each time around the loop  

     for ($i = 0, $j = count($chunks); $i < $j; $i++) {  


     $seconds = $chunks[$i][0];  

     $name = $chunks[$i][1];  


     // finding the biggest chunk (if the chunk fits, break)  

     if (($count = floor($since / $seconds)) != 0) {  





     $print = ($count == 1) ? '1 '.$name : "$count {$name}s";  


     if ($i + 1 < $j) {  

     // now getting the second item  

     $seconds2 = $chunks[$i + 1][0];  

     $name2 = $chunks[$i + 1][1];  


     // add second item if its greater than 0  

     if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {  

         $print .= ($count2 == 1) ? ', 1 '.$name2 : " $count2 {$name2}s";  



     return $print;  



Open in new window

Question by:informabiz
  • 4
LVL 108

Expert Comment

by:Ray Paseur
ID: 24805153
On my twitter account, these are the images of me.

When I use this link:
... I get an XML file that sends an error message.  See code snippet.

So it would appear that you need to extract the Twitter name as well as the subdirectory key from the /profile_images/ directory.

Sorry if that is not much help, but at least it helps establish a pattern,  Best, ~Ray


<Message>The specified key does not exist.</Message>







Open in new window


Author Comment

ID: 24915606
Hi Ray,

Is there any sets of rules for names like user_id.jpg or images are named randomly ?
LVL 108

Accepted Solution

Ray Paseur earned 500 total points
ID: 24915876
It can't be completely random, but it may not be very predictable.  Here is Andy Murray's "large" photo link

It looks like this much is consistent:

After that, the file names may be something that is an aritfact of the image upload - probably carried in the Twitter data base.
LVL 108

Expert Comment

by:Ray Paseur
ID: 25109166
Not sure what to do about this.  I believe our Asker is looking for information that Twitter may not want to make public, so there may be no good way to answer the Q.  Twitter has an API (and I would post a link here, but Twitter is down at the moment - a not uncommon situation).
LVL 108

Expert Comment

by:Ray Paseur
ID: 25109302
Worth a look

I recently developed some automated Twitter interface code.  I am now in the process of revising and strengthening the error recover components!  ;-(

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now