TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   XBL Gamercard Fetching Class (http://www.talkphp.com/advanced-php-programming/1305-xbl-gamercard-fetching-class.html)

obolus 10-15-2007 02:42 AM

XBL Gamercard Fetching Class
 
Found this at simes.org. It's a class for fetching Xbox Live Gamertags. Pretty neat. I haven't looked through the entire code yet to see if there's stuff that needs to be fixed, etc. I'm new to OOP in PHP5, but this looks like it was written for PHP4. Someone correct me if I'm wrong. Enjoy.

PHP Code:

<?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;
}

?>


Salathe 10-15-2007 09:27 AM

From a quick glance, yes it's almost certainly 'old' PHP4 code. It could do with being updated for PHP5. :)

obolus 10-15-2007 10:10 AM

Hmm!

If I find time this week, I'll give rewriting it for php5 a whirl.

Wildhoney 10-15-2007 11:38 AM

Sadly, it looks like PHP4 to me as well.

bluesaga 10-24-2007 09:58 AM

Thats probably the most useless implementation of a class i have ever seen.

A whole class for just defining variables? Has no one heard of define()?

Salathe 10-24-2007 11:46 AM

It's the object oriented approach, bluesaga. You don't have to like it, but having a wrapper for the Card details is a good thing in my eyes. Why the functions aren't methods of the class I've no idea though.

bluesaga 10-24-2007 11:50 AM

I just feel having a whole class for 7 variables is a bit weird when you don't wrap the functions within the class aswell, its like forcing half a script to use a class and the other half to not worry. Its just weird and bad coding practice to do so.


All times are GMT. The time now is 04:16 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0