Designer & Developer
I don't really like the style of the standard Xbox Live Gamercard. It's cool and all but I'd like to personalize it so it fits the design of my site, the standard Gamercard however does not. I searched for solutions to create my own Gamercard. I found some such as MyGamerCard where you can customize your own Gamercard but only limited to certain preconfigured styles. I figured there would be some sort of public API by Microsoft where I could just pull the data from and display it in a neat way, only to find out you have to be an official Xbox Community Developer to get access to this API. Bummer!
Then I came across Xbox Live Nation where the main developer, who has access to the official Xbox Gamercard API, created his own API which is public. With this Xbox Live Nation Gamercard API I created my own Xbox Live Gamercard and some of you have emailed me wondering how I did this. Therefore I wrote a little tutorial explaining it step-by-step so you can recreate it yourself with your own design. You have to be a member of Xbox Live Nation to get access to the API, so sign up before you start.
Step 1: Getting a Gamercard
When you have signed up at Xbox Live Nation you should be able to get data from the Gamercard API. All you have to do is to download the XML feed once in a while to your own server since you don't want to send a request to the API everytime someone visits your site. I'm using the XML feed URL http://www.xboxlivenation.com/api/xlnapigt.xml?gt=sizzlingjordi, but obviously replace sizzlingjordi with your own Xbox Gamertag to retrieve your Gamercard. I'll be using PHP to fetch the XML document with CURL, then I save the document on my server. I am going to display my Gamercard in Flash and therefore I rather have a JSON feed instead of XML so I'll convert the XML data to JSON data also so I can have easy access to the Gamercard data in Actionscript. I'm using the XML2JSON library for the conversion (download JSON.php, download xml2json.php)
Step 2: Get your hands dirty...
First, create a new file xboxlive.php and enter the following:
$getnew = true; $content = ""; $file = "/www/xboxlive/xboxlive.xml"; $gamertag = "sizzlingjordi"; if (file_exists($file)) { $filetime = filemtime($file); $time = time(); $difference = ($time - $filetime); if ($difference < 600) { $getnew = false; $content = file_get_contents($file); } }
You need to replace $file with the absolute path to the file you would like to write the JSON data to. Be sure that this file, or the directory the file should be created in, is writeable. Replace $gamertag with your own Gamertag. The code aboves basically checks if the file exists already and if it does it checks the time the file was updated. If this is below 10 minutes ago then it will display the contents of the file on the server since you don't want to make a request to the Gamecard API everytime your Gamercard gets shown.
Step 3: Fetch the XML data with CURL
if ($getnew === true) { $ch = curl_init(); $fp = fopen($file, "w+"); curl_setopt($ch, CURLOPT_URL, "http://www.xboxlivenation.com/api/xlnapigt.xml?gt=" . $gamertag); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, false); curl_exec($ch); curl_close($ch); header("Location: http://" . $_SERVER["SERVER_NAME"] . $_SERVER["SCRIPT_NAME"] . "?r=" . time()); exit(); }
The code above will download the XML data from the API using CURL and saves it to the XML file on your server. Then it redirects to itself so it can read the contents of the XML file right away. It might seem silly but it needs a redirect because I had some problems during testing as the XML file wasn't always fully updated when using file_get_contents() right after curl_close().
Step 4: Output as JSON
Using the XML2JSON library we can read the XML data and convert it to JSON, then display it so it's readable by Actionscript through the xboxlive variable.
require("includes/xml2json.php"); $content = xml2json::transformXmlStringToJson($content); print("&xboxlive=" . $content);
Step 5: Prepare your Flash file
All you need to do is prepare your Flash file so your Gamercard data will be displayed correctly. I created a MovieClip with instance name gamercard_mc. This MovieClip contains two textfields: gamercard_txt for displaying my Gamertag and details_txt for displaying my score, zone and the last game I played. Then I created another MovieClip within this MovieClip which serves as a placeholder for my Gamertag Picture called picture_mc. The width and height of the square Gamercard Picture is 64 pixels. Create your own design here to fit the design of your site or wherever you're going to place your Gamercard. Let's move on to the interesting stuff...
Step 6: Grand finale
Using Actionscript we can get the contents of the JSON file and parse the data directly using the JSON class for Actionscript. Place the following code in the first frame of the root of your Flash file (not in the gamercard_mc MovieClip).
import JSON; var feed:String = "http://www.jor-on.com/xboxlive.php"; var json:JSON = new JSON(); var xboxLive:LoadVars = new LoadVars(); xboxLive.onLoad = function(success:Boolean):Void { if (success) { var xl:Object = json.parse(this.xboxlive); gamertag_mc.gamertag_txt.text = xl.xlnapi.coreinfo.gamertag; gamertag_mc.details_txt.text = xl.xlnapi.coreinfo.score + "n" + xl.xlnapi.coreinfo.zone + "n" + xl.xlnapi.coreinfo.lastplayed; var pic_mcl:MovieClipLoader = new MovieClipLoader(); var tile:String = xl.xlnapi.coreinfo.tile; var tileArray = tile.split(" "); tile = tileArray.join("+"); pic_mcl.loadClip(tile, gamertag_mc.picture_mc.image_mc); gamertag_mc.picture_mc.link = xl.xlnapi.coreinfo.liveurl; gamertag_mc.picture_mc.onPress = function():Void { getURL(this.link); } } } xboxLive.load("http://www.jor-on.com/xboxlive.php?r=" + Math.random());
The code above creates a LoadVars object and loads the URL to the PHP script you just created so be sure to change the feed variable. Then the JSON class parses the data it got from the xboxlive variable. The picture will be downloaded and placed in a MovieClip called image_mc which is an empty MovieClip placed inside picture_mc you created earlier. The picture will get an onPress handler so the picture will be clickable and triggers a getURL function to your own Xbox.com Gamer profile.
And there you have it. Your own Gamercard. It's not perfect but it does the job pretty well and the way I like it. There is more data in the Gamercard API so take a look and try to expand it or maybe make the Flash file a little more interesting with animations or whatever you like
I am currently working as an Online Developer & Designer at MTV Networks Benelux. I live in Amsterdam in The Netherlands. I was born in 1983 and in 2007 I received my Bachelor of Engineering in Mediatechnology at the Hogeschool Utrecht, University of Applied Sciences. This website is part personal blog, part portfolio and part nonsense. If you would like to know more about me, drop me a line or add me on Hyves (Dutch) or LinkedIn.
Bilderback
January 20th, 2008 at 8:15 am
Thank you very much.I am exploring the gamertag arena and attempting to create my own custom gamercard script through php.
Now, I will begin exploring XML amd StonyArc’s API
GHosaPhat
February 3rd, 2008 at 6:01 am
This is really cool, but I have one question: What version of Flash did you create the .FLA file in? I downloaded the source, and I wasn’t able to open it in Flash 8. I’d like to play with this a little, but I don’t know Flash very well.
Jordi
February 3rd, 2008 at 2:03 pm
I created it in Flash CS3 so that’s why you are not able to open it in Flash 8. I uploaded a Flash 8 compatible version here: http://www.jor-on.com/blog/wp-content/uploads/2008/02/xboxlive_f8fla.zip
Good luck
Ramzinho
February 6th, 2008 at 4:05 am
Well.. i was trying to understand but it seemed to complicated to me.. as i am not a programmer..
I do XML editing and photoshop work for football manager on the FMGLive site. but this seemed to be very advanced as i didn’t even know how to use the CURL to capture the xml document. and i didn’t know what server you are talking about.
I might need to achieve my dream learning C++ first then come give this a try.
Anyway it’s way too kind of u to share such a thing and i just wanted to tell u good work.
Christian
February 9th, 2008 at 8:11 pm
Hello. I tried to follow your steps, but I don`t get it to work, so I downloaded your zip file. I get the following error message when I load xboxlive.php:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /MY URL/xml2json.php on line 73
Jordi
February 10th, 2008 at 12:44 pm
That’s weird, works fine here… I didn’t create that file myself, but your server probably needs PHP 5 installed… do you have PHP 5 or PHP 4?
OuterEight
February 26th, 2008 at 5:44 pm
Hey Jordi,
First off i’d like to say thanks for providing this amazing tutorial, i’ve been searching for a way to make my own gamercards for ages. Now for my question.
Is there a way, where you can get the backgrounds to change like MGC does to the corresponding game which you last played? It’d be awesome if you could write something which does this too.
// Eight.
Jordi
March 11th, 2008 at 9:27 pm
Hi OuterEight,
There is not really a way to change background to something that represents the last played game. The only information that’s in the feed is the title of the last played game and that’s it. I wish there was more information available, such as the last 5 played games, achievements per game, game icon, etc… but infortunately there isn’t
Estranged
April 10th, 2008 at 7:54 pm
Hey. Is there any way of designing skins without all the coding..
Like creating the artwork and layout for the contents, and the rest is automatically done.
jase
April 15th, 2008 at 1:46 pm
much easier way using this service http://www.gcdesigner.net …. you make your image in photoshop (or equivilent program), upload it to the web, and then totally customize your gamercard.