Skip to content

Instantly share code, notes, and snippets.

@webdevwilson
Created January 22, 2011 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save webdevwilson/791390 to your computer and use it in GitHub Desktop.
Save webdevwilson/791390 to your computer and use it in GitHub Desktop.
PHP API for Xbox Live
<?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]
);
}
}
}
<?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