Skip to content

Instantly share code, notes, and snippets.

@Jaybuz
Created October 15, 2009 13:36
Show Gist options
  • Save Jaybuz/210953 to your computer and use it in GitHub Desktop.
Save Jaybuz/210953 to your computer and use it in GitHub Desktop.
Show your xboxGamercard.
/* Xbox 360 Gamercard */
.gamercard * {
padding: 0;
margin: 0;
}
.gamerCard {
/* float: left; */
background: #fefffe url('imgs/layout_news_body.gif') no-repeat right top;
border: 1px solid #b2b2b2;
width: 198px;
padding: 4px;
margin: 10px 10px 0 0;
}
.gamerCard h2 {
font-size: 14px;
margin: 8px 0 0 0;
}
.gamerCard #avatar {
float: left;
height: 64px;
width: 64px;
margin: 3px 8px 0 3px;
}
.gamerCard p span {
color: #666;
}
.gamerCard #online {
color: #53af36;
}
.gamerCard #offline {
color: #af3636;
}
.gamerCard .recentGames {
background: #ffffdf;
border-top: 1px dotted #b2b2b2;
height: auto;
padding: 3px;
margin: 12px -4px -4px;
}
.gamerCard .recentGames img {
border: 1px solid #ccc;
height: 32px;
width: 32px;
margin: 3px;
}
/* End Gamercard */
<?php
/*
Usage: <?php echo xboxGamercard('GamerTag'); ?>
The gamercard info is cached as a text file for speed and server load purposes. By default,
the cache will not be older than 15 Minutes.
You will have to adjust the path to the images folder.
Original Idea: http://www.ardamis.com/2007/03/29/xbox-360-gamercard-wordpress-plugin/
*/
function xboxGamercard($tag = 'JAY BUZ', $gamesAmount = 10)
{
$dir = 'data/XboxGamerTags/GC-Cache-';
$timeout = 15; // minutes
$tagHTML = urlencode($tag);
// Makes the file if it doesn't exist
file_put_contents($dir.$tag.'.txt', '', FILE_APPEND);
$file = file($dir.$tag.'.txt');
$file = array_map('trim', $file);
$abstand = time() - $file[0];
$data = @file_get_contents('http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag='.$tagHTML);
if (!ini_get('allow_url_fopen')) die('file_get_contents() cannot connect to other websites, please contact administrator about this!');
if ($abstand > $timeout*60 || $tag != $file[1] || $data)
{
$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
$gamerTag = $tag;
$gamerMembership = $xml->AccountStatus;
$match = preg_match('#seen (.+?) (ago| playing)#', $xml->PresenceInfo->Info, $gameStatusTime);
$gameStatusTime = $match ? ', '.$gameStatusTime[1] : '';
$gamerStatus = $xml->PresenceInfo->Online == 'true' ? '<span id="online">Online</span>' : '<span id="offline">Offline'.$gameStatusTime.'</span>';
$gamerTile = $xml->TileUrl;
$gamerRep = round(($xml->Reputation)/5);
$gamerScore = $xml->GamerScore;
$gamerZone = $xml->Zone;
$games = $xml->RecentGames->XboxUserGameInfo;
$i = 1;
foreach ($games as $game)
{
$image = $game->Game->Image32Url;
$name = $game->Game->Name;
$myScore = $game->GamerScore;
$totalScore = $game->Game->TotalGamerScore;
if($i <= $gamesAmount)
$gameList .= '<img src="'.$image.'" title="'.$name.' (G - '.$myScore.'/'.$totalScore.')" alt="" />';
$i++;
}
$cacheString = time()."\n".
$tag."\n".
$gamerMembership."\n".
$gamerStatus."\n".
$gamerTile."\n".
$gamerRep."\n".
$gamerScore."\n".
$gamerZone."\n".
$gameList;
file_put_contents($dir.$tag.'.txt', $cacheString);
}
else
{
$tagHTML = $file[1];
$gamerTag = $tag;
$gamerMembership = $file[2];
$gamerStatus = $file[3];
$gamerTile = $file[4];
$gamerRep = $file[5];
$gamerScore = $file[6];
$gamerZone = $file[7];
$gameList = $file[8];
}
$gamerRep = "../imgs/xboxRep/rep".$gamerRep.".png";
$output = '
<div class="gamerCard">
<img id="avatar" src="'.$gamerTile.'" />
<h2><a href="http://profile.mygamercard.net/'.$tag.'">'.$gamerTag.'</a></h2>
<p><small>GamerScore: <span>'.$gamerScore.'</span><br />'.$gamerStatus.'</small></p>
<div class="recentGames">
'.$gameList.'
</div>
</div>'."\n";
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment