Home of the Ultimate Xbox 360 Modding Tool, Horizon. XboxMB.com is a community of Xbox 360 gamers and modders who share Tutorials, News, Reviews, and other resources. Xbox Message Boards is free to sign up and use, so what are you waiting for? Register Now!
In this tutorial i'll show you how to easily parse XboxMB's API via PHP.
first off, let's define our variables.
PHP Code:
$api_url = 'http://api.xboxmb.com/?u=';
// Use $_REQUEST to get either post or get data. In this case we will expect u to be the username $username = $_REQUEST['u'];
Now, There are a couple of methods to retrieve the raw output in which the api gives. You can be lazy, and go for file_get_contents(); but CURL is proven to be faster so i'll show you CURL.
PHP Code:
//init the CURL request. $info = curl_init();
//set the session to retrieve from this url. the period concates both variables ($api_url.$username) curl_setopt($info, CURLOPT_URL, $api_url.$username);
//set the session to return data from the sessions's url. curl_setopt($info, CURLOPT_RETURNTRANSFER, true);
//sets the variable $result to contain the returned data. $result = curl_exec($info);
//closes the sessions connection. curl_close($info);
Some people don't have CURL installed on their servers, so file_get_contents() would be your best bet. Instead of the CURL above, use this.
PHP Code:
$result = file_get_contents($api_url.$username);
Now we have raw data of the requested username. Since the API's response is JSON we will have to decode it into usable form. The simplest and only method that i know of, is json_decode.
PHP Code:
//set the $result variable to be the decoded JSON. The 'true' parameter puts the data into an array. I HIGHLY recommend it, as if you have worked with arrays it makes parsing it a piece of cake. $result = json_decode($result,true);
//now that we have all the data given by the API in a easily accessible array, let's assign some variables.
//is the user valid? $valid = $result['Valid'];
//set the correctly formatted username (Capitalization). $username = $result['Username'];
I took this php script and put it inside a easy to use PHP class if anyone want's it
here it is
PHP Code:
<?php
/*
This PHP class will make it easy for you to fetch data from the XboxMB api.
Written by: sgtfrankieboy
Thanks to:
XboxMB Staff - For this great API
xAb - For making a great tutorial
*/
class XboxMB {
public $Valid;
public $Username;
public $Title;
public $Staff;
public $Diamond;
public $Banned;
public $Posts;
public $Thanks;
public $Gamertag;
public $Joined;
public $UID;
//Create a new instance of XboxMB API class
public function __construct($username) {
$this->Load($username); //Load in a user
}
//Load a new user
public function Load($username)
{
//init the CURL request.
$info = curl_init();
//set the session to retrieve from this url.
curl_setopt($info, CURLOPT_URL, "http://api.xboxmb.com/?u=" . $username);
//set the session to return data from the session's url
curl_setopt($info, CURLOPT_RETURNTRANSFER, true);
//sets the variable $result to cotathe returned data.
$result = curl_exec($info);
curl_close($info);
$result = json_decode($result, true);
//is the user valid?
$this->Valid = $result['Valid'];
//set the correctly formatted username (Capitalization).
$this->Username = $result['Username'];
Programming today is a race between software engineers striving to build bigger and better
idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far, the Universe is winning.