Xbox Live Gamer Status

UPDATED December 1, 2009

Hey guys, just to let you know. If you were having troubles with the source code below, you may want to give it another go. The url for the API had changed and I hadn’t noticed it until now. Please copy and paste the code below into a new php file and give it a whir. Please let me know if you have any other problems. I am currently working on a very cool project that I think many of you will use in the future. Let’s just say that it will be the envy of all your xbox friends who don’t use Wordpress. Enjoy.


Xbox Live is a super cool gaming platform. Not only that, its a great way to interact with your friends and others on a social level. I have been appalled at the fact that the Xbox development team has not made it easier for us to let our friends know when we are connected to Xbox Live.

Sure you can login to xbox.com or possibly use your iPhone friends list application, but what about a way to display your Online Status anywhere on the web. Well here is your answer. After about of day of research and development I have come up with a solid solution that can be styled anyway you choose.


Unstyled Code:

Alt text
It turns out that one of the Xbox developers, Duncan Mackenzie has written a very valuable script that gathers a ton of information about Xbox Live users including: their online status, what games they have played recently and their achievement scores in each game. The problem is, he doesn’t explain in detail how to use his “Gamertag API” feed.

After understanding what all cURL was all about and how to parse through XML files using SimpleXML, I finally came up with my solution.


Styled Code:

Alt text

I advise you to take a look at my code and try it for yourself. I have also listed a few small requirements below the source code.

Also, be sure to install the PHP widget if you would like to run this code inside a widget on your sidebar.


Embed your Xbox Live status with PHP.

<?php

$ch = curl_init("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=YOURGAMERTAG");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);

$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);

foreach ($xml->PresenceInfo as $mystatus) 
{
print  '<div id="xboxlivestatus"><a href="' . $xml->ProfileUrl . '">' . $xml->Gamertag . '</a> is ' . $mystatus->StatusText . ' : ' . $mystatus->Info . '</div>';
}

?>

Requirements:

  • You must be running PHP 5
  • You must have cURL enabled
  • Change YOURGAMERTAG to your personal gamertag

If you have any questions please leave a comment.

Post to Twitter

40 Responses to “Xbox Live Gamer Status”

  1. Davis  on January 24th, 2009

    Pretty slick man. I wish this post applied to me. I don’t have a 360.

  2. Kev  on February 6th, 2009

    amazing, i was hoping to develop something for facebook that uses all this info. hmmmm!

  3. Nyne  on February 21st, 2009

    this is awesome and just what i am looking for sort of. I was wondering what needed to be done for this to complete a little more quickly and just get the online/offline status. you can probably just pull the initial gamertag xml data and directly access the variable data instead of iterating threw but i dont know how to do that. Thanks for help in the matter!

  4. George  on March 2nd, 2009

    Hi Mike, sorry to bother ya about this, but is there a syntax error in that logic above? I’m running PHP5 with curl enabled, and I’m getting … Parse error: syntax error, unexpected T_STRING in /home/rcfeasyc/public_html/HORHAY_WP/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 20

    Seems to be isolated to the foreach loop when I was debugging… can’t seem to figure out what’s causing it

  5. Michael  on March 2nd, 2009

    No problem man. I don’t think there is a logic issue though. I have had several people run the exact code above with no issues. Where are you trying to place your code? In the sidebar? Make sure you download and install the sidebar PHP widget if that is the case. That should fix your problems.

    Let me know if you have any more issues. I am looking for a new way to write the code so that it doesn’t take forever to load your page once it’s implemented. So look for an update coming out soon.

  6. George  on March 2nd, 2009

    Thanks Mike, I’m actually using wordpress 2.7, and I’m trying to code it directly into a page. I’m using the editor, but I’ve installed Exec-php plugin to allow for php code entry directly into the editor. So far it’s been working great and I haven’t had any syntax issues. I tried to break down the code as much as possible, and everything’s fine up until the loop. I’m just picking up PHP fairly recently again so I’m a bit of a novice learning it all over again. I’ll try fiddling around with it to see if it can be resolved, and if it’s something wordpress specific, I’ll let you know just as an fyi. I’ll just try throwing it into a regular file and trying to retrieve it on the server. If it comes back fine, it’s got to be wordpress. Def looking forward to an updated version.

  7. Morty  on April 1st, 2009

    Quite useful actually. I was wondering if one could find the entire list of achievements from a user in particular. Does that sound possible to you with this API?

    Thanks in advance,
    Morty

  8. Tactics  on May 21st, 2009

    Hi there,

    When I try to use the code, I get this error:

    Parse error: parse error, unexpected T_STRING in D:\hosting\member\******\xweb\XboxLive\test.php on line 17

    I have all requirements enabled.

  9. Tactics  on May 23rd, 2009

    Sorry for the second post, but im still having trouble? This line seems to be the problem for me:

    Text . ‘ : ‘ . $mystatus->Info . ‘ ‘;

  10. Vinny  on August 9th, 2009

    hi,
    how do i get other info like Gamertag and rep etc

  11. Jaybuz  on August 9th, 2009

    Just use:

    $xml->PresenceInfo->StatusText;
    $xml->PresenceInfo->Info;

    Instead of the foreach loop.

  12. unholybizkit  on December 1st, 2009

    i keep getting
    Parse error: parse error in C:\wamp\www\test.php on line 22

  13. unholybizkit  on December 1st, 2009

    it is on line 22 because i used
    $xml->PresenceInfo->StatusText;
    $xml->PresenceInfo->Info;

    instead of foreachloop

  14. unholybizkit  on December 1st, 2009

    i am also getting T_string error tell me if i did something wrong here this is the entire page called test.php. i am going to try and add the html body and head tags and see if that fixes it

    PresenceInfo->StatusText;
    $xml->PresenceInfo->Info;
    {
    print ” . $mystatus->Status
    Text . ‘ : ‘ . $mystatus->Info . ‘ ‘;
    }

    ?>

  15. Michael  on December 1st, 2009

    Sorry Nick. I haven’t kept up with this code in over a year. I know it stopped working a while back, so if you get it up and running again, let me know. I probably wont be much help though.

  16. Michael  on December 1st, 2009

    Alright folks. Updated the code. Feel free to try it again.

  17. unholybizkit  on December 2nd, 2009

    hey thanks man i edited a little so it adds some more info but to tell you the truth i am not very good with php but looking at scripts help me learn

  18. Michael  on December 2nd, 2009

    Glad to hear it! Would you mind linking me to your implementation. I would love to see how you are using it.

  19. Vitamin R  on December 13th, 2009

    Hey Micheal, thanks a million for the code, but I’m trying to take it one step farther.

    I want to apply the code to each user of my site, using the $userdata[user_gamertag] variable, but there seems to be all kinds of T-String errors. It’s weird and I’m really confused as to what I am doing wrong. If I fix the errors, then the code displays incorrectly and says the variables are undefined. Quite the mess.

    Any help would be appreciated.

  20. unholybizkit  on January 1st, 2010

    well i haven’t touched it in a while i have been working on a site and needed to get the right look ya know images and stuff but i will upload it shorlty

  21. Tim  on January 4th, 2010

    I can’t seem to get it to work with a gamertag that has spaces in it. Any ideas?

  22. Tim  on January 5th, 2010

    Never mind, that was a dumb question, I just used str_replace.

  23. Michael  on January 5th, 2010

    Hey there Tim. Glad you worked it out. Just wanted to let you know that you simply use a plus (+) sign rather than spaces, you will be good to go. Thanks for playing around with the script!

  24. Tim  on January 6th, 2010

    Yeah, thanks. I actually had one more question. Would it be possible to change the default “?????? is :” when there is an invalid gamertag to something else?

  25. Tim  on January 11th, 2010

    Here is my little project I have been working on. I utilized your code (with a little extra of my own) to allow users to quickly check the status of people on xbox live. Check it out (i’m still working on the logo)- xblstatus.com

  26. Dave  on January 21st, 2010

    Is there someway I could parse this into flash? thanks.

  27. Dave  on January 23rd, 2010

    Why do I keep getting this error?

    Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /home/mcewc/public_html/member1.php:91 Stack trace: #0 /home/mcewc/public_html/member1.php(91): SimpleXMLElement->__construct(”, 16384) #1 {main} thrown in /home/mcewc/public_html/member1.php on line 91

  28. Tim  on January 23rd, 2010

    Yeah, I keep getting the error as well. It’s been working fine till now, maybe a url changed.

  29. Dave  on January 25th, 2010

    Tim are you available for design work? I really like how you made your gamertag checker from using duncans API.

    I would be willing to pay you if you made me something similar.

    Please email me if interested :)

    mcgovery@live.com

  30. Dave  on January 25th, 2010

    Also does anyone know why the api takes so long to update? For example sometimes it takes upto an hour to update gamers status.

  31. Kevin  on January 29th, 2010

    Dave, I was wondering the same thing as well. My son has been logged off of Xbox live for 29 minutes now, but the API still shows him as online. I can understand limiting the updates so the API server doesn’t get hammered every 30 seconds, but 29 minutes? Seems kind of useless if the data doesn’t update.

    Does anyone know the minimum refresh time for the API?

  32. Kevin  on January 29th, 2010

    Does anyone know the minimum update time for the API? I just checked an update status for an account logged off over 30 minutes ago, and the API still thinks the account is online.

  33. Dave  on January 30th, 2010

    Looks like nobody even replies here anymore :S.
    The update time for the API is pretty worthless indeed.

  34. Tim  on January 30th, 2010

    Yeah, its weird. Sometimes it updates perfectly yet other times it takes forever.

  35. JoshB  on February 1st, 2010

    Is there any possible way to get this to run as a signature on a forum? You know…so people can simply see whether or not you are online? (sorry…not very technologically inclined) Thanks. Josh

  36. Pembo  on March 2nd, 2010

    Sweet!

  37. Mattias  on March 5th, 2010

    This was really nice, thanks a lot! I have a question though (someone else asked it above, but I couldn’t find any answer):

    I use it on my website, a Fifa 10 League dedicated site, and I have now coded so the users can see their opponents XBL status. However, the page takes so much time to load, is it possible to only retrieve the Online/Offline information so it takes less time to load?

    Thanks!!!

  38. Michael  on March 5th, 2010

    Hi Mattais, thanks for the question. Unfortunity there is not a way to only retrieve a certain amount of info as of right now. I am currently working on a way to accomplish this.

    The problem is that Duncan’s API retrieves all info related to the gamertag and allowing you to show what you want. The API needs to be updated to allow you to only retrieve values you want rather than giving you everything and making you hide or show what you want.

    One way around this would be to load the API once then have the server cache it for 20 or 30 min. That way, every page load wouldn’t take forever, just the initial one.

  39. Mattias  on March 5th, 2010

    Ok, thanks a lot for your answer! Do you know how frequently it updates? Because it doesn’t seem to update so often. Is this something you can affect or is that impossible?

    Thanks!


Leave a Reply