Created
January 22, 2011 19:45
-
-
Save webdevwilson/791390 to your computer and use it in GitHub Desktop.
PHP API for Xbox Live
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('URL_PREFIX', 'http://gamercard.xbox.com/en-US/'); | |
define('URL_AFFIX', '.card'); | |
define('SUBSCRIPTION_GOLD', 'Gold'); | |
define('SUBSCRIPTION_SILVER', 'Silver'); | |
class XboxGamercard { | |
public $gamertag; | |
public $html; | |
public $subscription; | |
public $gamerscore; | |
public $games; | |
public function __construct($gamertag) { | |
$this->gamertag = $gamertag; | |
$this->refresh(); | |
} | |
public function refresh() { | |
$url = URL_PREFIX . rawurlencode($this->gamertag) . URL_AFFIX; | |
$h = file_get_contents($url); | |
if($h === false) { | |
throw new Exception("Invalid gamertag or service not available"); | |
} | |
$this->html = $h; | |
$this->subscription = stristr($h, "<span class=\"Gold\">") === false ? | |
SUBSCRIPTION_SILVER : SUBSCRIPTION_GOLD; | |
$this->gamerscore = preg_match('/<div class="Stat">([0-9]+)<\/div>/', $h, $m); | |
$this->gamerscore = intval($m[1]); | |
$this->games = array(); | |
$re = '/<img class="Game" width="32" height="32" src="(.+)" alt="(.+)" title="(.+)" \/>/'; | |
preg_match_all($re, $h, $m); | |
for($i = 0; $i < count($m[1]); $i++ ) { | |
$this->games[] = array( | |
'title' => $m[3][$i], | |
'tile' => $m[1][$i] | |
); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'XboxGamercard.class.php'; | |
define('TEST_GAMERTAG', 'Major Nelson'); | |
echo "Testing XboxGamercard Class\n"; | |
$failed = array(); | |
set_error_handler( function($num, $string) { $failed = true; echo "{$errstr}\n"; } ); | |
set_exception_handler( function($e) { $failed = true; echo "{$e->getMessage()}\n"; } ); | |
$gamercard = new XboxGamercard(TEST_GAMERTAG); | |
equals($gamercard->subscription, SUBSCRIPTION_GOLD); | |
equals(count($gamercard->games), 5); | |
greaterThan( $gamercard->gamerscore, 0 ); | |
assert(count($failed) == 0); | |
function greaterThan($value, $expected) { | |
if(! $value > $expected ) { | |
fail("Expected: > {$expected}, Got: {$value}"); | |
} else { | |
passed(); | |
} | |
} | |
function equals($value, $expected) { | |
if( $value != $expected ) { | |
fail("Expected: {$expected}, Got: {$value}"); | |
} else { | |
passed(); | |
} | |
} | |
function fail($msg) { | |
global $failed; | |
echo $msg . "\n"; | |
$failed[] = $msg; | |
} | |
function passed() { | |
echo "Test passed.\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment