TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-15-2007, 02:42 AM   #1 (permalink)
The Acquainted
 
obolus's Avatar
 
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
obolus is on a distinguished road
Default 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;
}

?>
obolus is offline  
Reply With Quote
Old 10-15-2007, 09:27 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

From a quick glance, yes it's almost certainly 'old' PHP4 code. It could do with being updated for PHP5. :)
Salathe is offline  
Reply With Quote
Old 10-15-2007, 10:10 AM   #3 (permalink)
The Acquainted
 
obolus's Avatar
 
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
obolus is on a distinguished road
Default

Hmm!

If I find time this week, I'll give rewriting it for php5 a whirl.
obolus is offline  
Reply With Quote
Old 10-15-2007, 11:38 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Sadly, it looks like PHP4 to me as well.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-24-2007, 09:58 AM   #5 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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()?
bluesaga is offline  
Reply With Quote
Old 10-24-2007, 11:46 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 10-24-2007, 11:50 AM   #7 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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.
bluesaga is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:30 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design