<?php
class GamerCard
{
    var 
$memberType;

    var 
$tag;
    var 
$rep;
    var 
$score;
    var 
$zone;

    var 
$recentlyPlayed;

    var 
$gamerPictureUrl;


}

function 
error_handler($errno$errstr)
{
}

function 
getGamerCard($gamerTag)
{
    
$url "http://gamercard.xbox.com/" urlencode($gamerTag) . ".card";
    
// Grab the gamercard - the card HTML is not valid xml (the base tag is not closed) 
    // but the "body" tag and its contents are a valid xml doc so we throw away the contents of the "head" tag. 
    // We don't need them anyway.
    
//    $request = new HTTPRequest($url);
//    $cardHTML = $request->DownloadToString();
    
set_error_handler("error_handler");
    
$cardHTML file_get_contents($url);
    
restore_error_handler();
    if (
$cardHTML == false)
    {
        return 
"";
    }
    
$tmp preg_split("/<\/head>/"$cardHTML);
    
$tmp preg_split("/<\/html>/"$tmp[1]);
    
$cardHTML preg_replace(array("@<script[^>]*?>.*?</script>@si""@<noscript[^>]*?>.*?</noscript>@si"), array(""""), $tmp[0]); // Strip SCRIPT tags - not valid XML


    
$xml domxml_open_mem("<?xml version='1.0' standalone='yes'?>" $cardHTML);
    if (!
$xml)
    {
        print 
"Error parsing XML";
        exit;
    }

    
$root $xml->document_element();
    
$xpath $xml->xpath_new_context();

    
$card = new GamerCard();
    
$card->tag $gamerTag;

// Membership type (Gold/Silver)

    
$elems $xml->get_elements_by_tagname("h3");
    foreach(
$elems as $elem)
    {
        
$class $elem->get_attribute("class");

        if (
$class == "XbcGamertagGold")
            
$card->memberType "Gold";
        else if (
$class == "XbcGamertagSilver")
            
$card->memberType "Silver";
    }

// Gamer picture
    
$obj $xpath->xpath_eval('//img[@class="XbcgcGamertile"]');
    
$nodeset $obj->nodeset;

    
$card->gamerPictureUrl $nodeset[0]->get_attribute("src");

// Gamerscore

    
$obj $xpath->xpath_eval('//span[preceding-sibling::span/img[@alt="Gamerscore"]]');
    
$nodeset $obj->nodeset;
    
$card->score $nodeset[0]->get_content();

// Rep

    
$obj $xpath->xpath_eval('//span[preceding-sibling::span="Rep"]/img');
    
$nodeset $obj->nodeset;

    
$url $nodeset[0]->get_attribute("src");
    
$tmp preg_split("/[._]/"$url);
    
$card->rep $tmp[3];

// Zone

    
$obj $xpath->xpath_eval('//span[preceding-sibling::span="Zone"]');
    
$nodeset $obj->nodeset;
    
$card->zone $nodeset[0]->get_content();

// Recently Played

    
$obj $xpath->xpath_eval('//div[@class="XbcgcGames"]//img');
    
$nodeset $obj->nodeset;
    
    foreach(
$nodeset as $node)
    {
        
$card->recentlyPlayed[] = $node->get_attribute("title");
    }

    return 
$card;
}

?>