Halo 3 Stats

By sean on Sep 16, 2008

this will retrieve your halo 3 weapon kills and medals from social rankings. the image id and how many kills/medals gets spit out via array ( example of how to use image id within snippet ).
made this for a facebook app that im creating and decided to share. this snippet is not mysql reliant, it stores everything within the specified cache file ('xbl_h3_cache.txt').

Usage: xbl_h3.php?gt=
demo: http://seantristin.com/testlab/xbl/xbl_h3.php?gt=sT%20threeZ

<pre>
<?php
$cache_file = 'xbl_h3_cache.txt';
$cache = ( file_exists($cache_file) ? unserialize(file_get_contents($cache_file)) : array() );

print("\n\nRecent Gamertags looked up:\n  ");
foreach ( $cache as $gt => $a )
    printf(' <a href="xbl_h3.php?gt=%s">%s</a> (%ss ago) ', $gt, $gt, time() - $a[0]);

if ( !isset($_GET['gt']) )
    die('no gt');
if ( !strlen($_GET['gt']) )
    die('no gt');

if ( !isset($cache[$_GET['gt']]) )
{
    $cache[$_GET['gt']] = array(0, NULL);
}

if ( $cache[$_GET['gt']][0] < (time() - 99) )
{
    $start = microtime(true);
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, 'http://www.bungie.net/stats/halo3/CareerStats.aspx?player=' . urlencode($_GET['gt']) . '&social=true&map=0');
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9) Gecko/2008062315 (Gentoo) Firefox/3.0.1');
    $o = curl_exec($c);
    //$o = @iconv('UTF-8', 'ISO-8859-1', $o);
    if ( ($ea = curl_errno($c) || $eb = curl_error($c)) )
        die("error 0x00");
    curl_close($c);
    $took = round(microtime(true) - $start, 2);
    print("\n\n[ ] Cache (0s old)   [&times;] Bungie (took {$took}s)\n\n");
    //file_put_contents('xbl_h3_o.txt', $o);
    $e = '/\<div .*\>[.\r\n\t ]*\<div .*\>\<img .* src\="\/images\/halo3stats\/weapons\/(.*)\.gif" .* \/\>\<\/div\>[.\r\n\t ]*\<div .*\>\<div .*\>(.*)\<\/div\>\<div .*\>(.*)\<\/div\>\<\/div\>[.\r\n\t ]*\<div .*\>\<div .*\>Total Kills\<\/div\>\<div .*\>(.*)\<\/div\>\<\/div\>[.\r\n\t ]*\<\/div\>/';
    if ( !preg_match_all($e, $o, $m) )
        die("error 0x01");
    unset($m[0]);
    $x = count($m[1]);
    for ( $i = 0; $i < $x; $i++ )
    {
        $guns[$m[2][$i]] = array('ImageId' => $m[1][$i], 'Kills' => $m[4][$i]);
        //print("<img src='http://www.bungie.net/images/halo3stats/weapons/{$m[1][$i]}.gif' />");
    }
    $e = '/\<div .*\>[.\r\n\t ]*\<div .*\>\<img .* src="\/images\/halo3stats\/medals\/(.*).gif" .* \/\>\<\/div\>[.\r\n\t ]*\<div .*\>\<div .*\>(.*)\<\/div\>\<div .*\>(.*)\<\/div\>\<\/div\>[.\r\n\t ]*\<div .*\>\<div .*\>Game Total\<\/div\>\<div .*\>(.*)\<\/div\>\<\/div\>[.\r\n\t ]*\<\/div\>/';
    if ( !preg_match_all($e, $o, $m) )
        die("error 0x02");
    unset($m[0]);
    $x = count($m[1]);
    for ( $i = 0; $i < $x; $i++ )
    {
        $medals[$m[2][$i]] = array('ImageId' => $m[1][$i], 'MedalCount' => $m[4][$i]);
        //print("<img src='http://www.bungie.net/images/halo3stats/medals/{$m[1][$i]}.gif' />");
    }
    $cache[$_GET['gt']] = array(time(), $guns, $medals);
    file_put_contents($cache_file, serialize($cache));
}
else
{
    $guns = $cache[$_GET['gt']][1];
    $medals = $cache[$_GET['gt']][2];
    $cache_age = time() - $cache[$_GET['gt']][0];
    print("\n\n[&times;] Cache ({$cache_age}s old)   [ ] Bungie (took 0s)\n\n");
}
print_r($guns);
print_r($medals);
?>
</pre>

<style type="text/css">

* { margin: 0; padding: 0; }

</style>

Comments

Sign in to comment.
Hawkee   -  Sep 19, 2008

I hope to have it done within the next month or so.

 Respond  
sean   -  Sep 19, 2008

nice! when do you think that'll be up and running?

 Respond  
Hawkee   -  Sep 16, 2008

We're building an OpenSocial platform here at Hawkee so you'll be able to submit it once we're ready.

 Respond  
sean   -  Sep 16, 2008

yea making an opensocial app wouldn't be a bad idea, i didn't really think of that. finishing up another app that works relatively the same except it will retrieve stats from your xbox live my gamercard ( played games, achievements etc )

 Respond  
napalm`   -  Sep 16, 2008

Yep. I need to get my 360 fixed. My stats are the suck.

 Respond  
Hawkee   -  Sep 16, 2008

This could be the basis to a good OpenSocial app. Could make it a profile widget for users to show off their stats.

 Respond  
napalm`   -  Sep 16, 2008

gamertag: thenapalmtheory

 Respond  
Hawkee   -  Sep 16, 2008

Cool, too bad you have to parse the HTML. Would be nicer if you had an API to connect to.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.