How To Display Twitter Profile Picture On My Website

Hi Everyone,

I've found interesting code at @
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24524469.html?sfQueryTermInfo=1+twitter

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

Can someone help?
<?php
$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("http://twitter.com/statuses/user_timeline/$name.rss"));
}
$items = simplexml_load_string( file_get_contents("http://twitter.com/statuses/user_timeline/$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="http://www.twitter.com/" property="foaf:accountName" href="http://twitter.com/<?php echo 
 
$name; ?>/">
<?php echo $name; ?></a></h1>
<div id="myTwitter">
<?php
	foreach( $items as $tweet) {
		$diff = Timesince(strtotime($tweet->pubDate));
		$link = $tweet->link;
		$post = preg_replace( '|([^\s])\s+([^\s]+)\s*$|', '$1&nbsp;$2',
						htmlentities(str_replace(array('&lt;','&gt;'),array('<','>'),
						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>";
	}
?>
</div>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){$("span").hide();(f=function(){$("span:hidden:first").fadeIn(600,f);})();});
//]]>
</script>
 
<?php
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) {  
         break;  
     }  
     }  
   
     $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

informabizAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
On my twitter account, these are the images of me.

http://s3.amazonaws.com/twitter_production/profile_images/299888078/Ray_Paseur_normal.png
http://s3.amazonaws.com/twitter_production/profile_images/299888078/Ray_Paseur_bigger.png
http://s3.amazonaws.com/twitter_production/profile_images/299888078/Ray_Paseur.png

When I use this link:
http://s3.amazonaws.com/twitter_production/profile_images/299888078/andy_murray.png
... 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
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>profile_images/299888078/andy_murray.png</Key>
<RequestId>607621F47FD061F2</RequestId>

<HostId>
qmkz7cY8EOZOnediXfic7dJAL3spfJyQyjfc7o1C0pn8QkfoNzdtOSkr5IPxLAu9
</HostId>
</Error>

Open in new window

0
informabizAuthor Commented:
Hi Ray,

Is there any sets of rules for names like user_id.jpg or images are named randomly ?
0
Ray PaseurCommented:
It can't be completely random, but it may not be very predictable.  Here is Andy Murray's "large" photo link
http://s3.amazonaws.com/twitter_production/profile_images/55512065/72990748.jpg

It looks like this much is consistent:
http://s3.amazonaws.com/twitter_production/profile_images/

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
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).
0
Ray PaseurCommented:
Worth a look http://status.twitter.com/

I recently developed some automated Twitter interface code.  I am now in the process of revising and strengthening the error recover components!  ;-(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.