theChrisWalker.net

I’m sorry we’re out of Cake. We only had 3 pieces and didn’t expect the rush…

Archive for the ‘PHP’ tag

Xbox Live Gamercard API

without comments

So, got decided to join most of my friends and I got an Xbox 360. Me being me though, I got interested in the way that all the information about your “Gamertag” is stored an accessible on the xbox.com website. Wouldn’t it be fun to do something with this data!

As it turns out, I was beaten to the post by Duncan MacKensie (http://duncanmackenzie.net/Blog/put-up-a-rest-api-for-xbox-gamertag-data) who hosts a webservice to retrieve gamer data from Microsoft. I could find no details about how this service works, where the data comes from or anything! Either he has a relationship with Microsoft, or he scrapes xbox.com but either way, the data seems pretty consistent and reliable. Actually it turns out this information was right there on his website… http://www.duncanmackenzie.net/Blog/if-you-are-wondering-where-i-get-my-xbox-live-info So he gets it as part of his membership to the Xbox Community Developer Program.

However, the webservice is great, and returns XML which is fine, but I thought it would be more useful to me to have a PHP API for this data. So I wrote one which retrieves data from Duncans webservice.

Then I thought wouldn’t it be great to be able to use this dynamically in a webpage, so I wrote a service frontend which will return JSON formatted data. Then I thought wouldn’t it be useful to let other people use this as well, so I modified it and it can now cope with JSONP requests with a “_callback” parameter.

OK, so what does this all mean.

The PHP

The class is called gamertag and the usage is very simple:

<?php
// include the class file
require "gamertag.php";

//instantiate
$G = new Gamertag('thechriswalker');

//get data
$data = $G->getArray();

print_r($G);

which outputs something like this:

Array
(
    [Gamertag] => thechriswalker
    [AccountStatus] => Silver
    [State] => Valid
    [ProfileUrl] => http://live.xbox.com/member/thechriswalker
    [TileUrl] => http://avatar.xboxlive.com/avatar/thechriswalker/avatarpic-l.png
    [AvatarFullUrl] => http://avatar.xboxlive.com/avatar/thechriswalker/avatar-body.png
    [Country] => United Kingdom
    [Location] => Bradninch
    [Bio] =>
    [Reputation] => 58.72229
    [ReputationImageUrl] => http://live.xbox.com/xweb/lib/images/gc_repstars_external_12.gif
    [Zone] => Recreation
    [GamerScore] => 230
    [PresenceInfo] => Array
        (
            [Valid] => true
            [Info] => Last seen 12/29/09   playing Modern Warfare® 2
            [Info2] =>
            [LastSeen] => Tue, 29 Dec 2009 21:35:22 +0000
            [Online] => false
            [StatusText] => Offline
            [Title] => Modern Warfare® 2
        )

    [RecentGames] => Array
        (
            [0] => Array
                (
                    [Name] => Modern Warfare® 2
                    [TotalAchievements] => 50
                    [TotalGamerScore] => 1000
                    [Image32Url] => http://tiles.xbox.com/tiles/Z+/tF/12dsb2JgbA9ECgQJGgYfVl5UL2ljb24vMC84MDAwIAABAAAAAPhq63g=.jpg
                    [Image64Url] => http://tiles.xbox.com/tiles/CE/Vx/0Gdsb2JhbC9ECgQJGgYfVl5UL2ljb24vMC84MDAwAAAAAAAAAP9eRRc=.jpg
                    [LastPlayed] => Tue, 29 Dec 2009 21:32:52 +0000
                    [Achievements] => 9
                    [GamerScore] => 115
                    [DetailsURL] => http://live.xbox.com/en-US/profile/Achievements/ViewAchievementDetails.aspx?tid=%09%5d%3a%60m%2fl%3b%7cw&compareTo=thechriswalker
                )

            [1] => Array
                (
                    [Name] => PGR 4
                    [TotalAchievements] => 60
                    [TotalGamerScore] => 1250
                    [Image32Url] => http://tiles.xbox.com/tiles/Y1/qn/0Gdsb2JgbA9ECgR8GgMfWSlaL2ljb24vMC84MDAwIAABAAAAAP+IWnw=.jpg
                    [Image64Url] => http://tiles.xbox.com/tiles/DP/ST/12dsb2JhbC9ECgR8GgMfWSlaL2ljb24vMC84MDAwAAAAAAAAAPi89BM=.jpg
                    [LastPlayed] => Mon, 28 Dec 2009 16:59:25 +0000
                    [Achievements] => 5
                    [GamerScore] => 115
                    [DetailsURL] => http://live.xbox.com/en-US/profile/Achievements/ViewAchievementDetails.aspx?tid=%09%5d%3a%15%18*iAq%0b&compareTo=thechriswalker
                )

        )

)

So now we can easily get at the data. The source code for the class (which is not fully tested, but the basics work!) is at http://thechriswalker.net/xbox360/gamertag.source.php (NB it requires either PHP5 (for json_encode) or the PEAR Services_JSON class if you want to use the “getJSON()” method).

The JSON

PHP is well and good but what if I want to use a JSON/JSONP (JSONP is for cross-domain information requesting and is very useful for public information services, see http://en.wikipedia.org/wiki/JSON#JSONP) request, well, that can be done at http://thechriswalker.net/xbox360/?gamertag=YOUR_GAMERTAG for the straight JSON or http://thechriswalker.net/xbox360/?gamertag=YOUR_GAMERTAG&_callback=YOUR_CALLBACK_FUNCTION_NAME for JSONP.

The first returns just JSON with a content type “application/json” and the second returns a javascript function call to your callback function with the JSON object as the only parameter and a content type of “text/javascript”.

These enabled me to build a simple Google Gadget to display a Gamercard:


Written by Chris

December 31st, 2009 at 11:34 am

Posted in Code

Tagged with , , , ,

CakePHP Evangelism and T-Cake

without comments

So I’ve had this blog for 3 posts now and none of them has a “cakephp” tag yet. Shocking. So here we go…

If you don’t know what CakePHP is and you’ve ever created a web application, or even a website that did more than show a static page, then you should check it out. CakePHP is a web application framework designed for rapid application development using the MVC Design Pattern and with a focus on Convention over Configuration.

What this means in practical terms is that you can built an access controlled, database driven, dynamic web sites/applications in a fraction of the time that it would to start from scratch. A simple blog could be built in a matter of minutes. I could go on for hours. So I won’t, I’ll talk about my latest project, which is – you guessed it! – built with CakePHP.

T-Cake

T-Cake is going to be a Torrent Controller. It’s not a Bittorrent Client, it can’t download using the bittorrent protocol or act as a tracker. It can provide an abstraction layer to a torrent client of you choice. Basically the application will allow remote access to control torrents which will then be activated using a torrent client.

The idea came from TorrentFlux which I used for a long while on my home server. However the time came when I wanted to do more, and was frustrated by the restrictions imposed by TorrentFlux being tied to BitTornado. The key idea in this whole project was choice of client.

To achieve this aim, I built a CakePHP datasource for interacting with a bittorrent client through an interface class. Then I designed an interface class for the Transmission Bittorrent Client which has a fairly simple JSON RPC for communication. I also intend to write an interface for rTorrent which has an XML RPC (slighty more complex, but potentially more functional).

The project is still in early stages, but going strong. The fact that I have abstracted the client, means I can add any features I want whether or not the client natively would support it, for example:

  • Seeding to a pre-determined limit
  • RSS torrent automation
  • configurable queuing system
  • Multi User option - with torrents only visible to their owner / admins
  • Direct Torrent search and acquire via “engine” plugins
  • Alerts for preconfigured events, e.g. torrent completion, new torrent added by RSS, new queue item starting, etc…
  • Alerts could be theorectically pushed to any medium, email, SMS, an RSS feed, etc…

However the proviso on the client is that it has an API or RPC that I can hook into with PHP.

Well, sounds exciting doesn’t it? I think so, I’ll post some screenshots in a bit, so you can see the start of the interface, and maybe some of the techniques I’ve used in CakePHP to make it work.

Written by Chris

January 28th, 2009 at 11:10 pm

Posted in Code

Tagged with , ,