Top

Halo 3 Stats


PHP Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  6.0 (of 1 scores)
Date Added  Sep 16, 2008
Last Updated  Sep 16, 2008
Tags  bungie  halo  php  stats 

Introduction

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=<GAMERTAG>
demo: http://seantristin.com/testlab/xbl/xbl_h3.php?gt=sT%20threeZ

Grab the Code

<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

  (8)  RSS
Hawkee
Comments: 1,039
 
PHP Snippet:  Halo 3 Stats
Posted on Sep 16, 2008 8:44 pm
Cool, too bad you have to parse the HTML. Would be nicer if you had an API to connect to.
napalm`
Comments: 182
 
PHP Snippet:  Halo 3 Stats
Posted on Sep 16, 2008 9:30 pm
gamertag: thenapalmtheory
Hawkee
Comments: 1,039
 
PHP Snippet:  Halo 3 Stats
Posted on Sep 16, 2008 9:38 pm
This could be the basis to a good OpenSocial app. Could make it a profile widget for users to show off their stats.
napalm`
Comments: 182
 
PHP Snippet:  Halo 3 Stats
Posted on Sep 16, 2008 9:51 pm
Yep. I need to get my 360 fixed. My stats are the suck.
sean
Comments: 109
 
PHP Snippet:  Halo 3 Stats
Posted on Sep 16, 2008 10:01 pm
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 )
Hawkee
Comments: 1,039
 
PHP Snippet:  Halo 3 Stats
Posted on Sep 16, 2008 10:26 pm
We're building an OpenSocial platform here at Hawkee so you'll be able to submit it once we're ready.
sean
Comments: 109
 
PHP Snippet:  Halo 3 Stats
Posted on Sep 19, 2008 7:26 am
nice! when do you think that'll be up and running?
Hawkee
Comments: 1,039
 
PHP Snippet:  Halo 3 Stats
Posted on Sep 19, 2008 9:07 am
I hope to have it done within the next month or so.

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom