Toribash
Original Post
[Tut] Retrieving Stats
Overview of obtaining the stats

A simple example and explanation of retrieving a users stats.
In this tutorial I will be using PHP to demonstrate, but you can use whatever language you like.

Firstly I want you to look at this URL:
http://forum.toribash.com/tori_stats...am&format=json
This will return a javascript/php valid json string containing the specified user's stats.

Like:
{ "username": "Blam", "userid": 19426, "games_played": 4813, "rank": 55, "qi": 4813, "wins": 2458, "loss": 641, "win_percentage": 79.316, "toricredits": 7380 }
JSON is similar to XML in that it represents data in a human readable form.

A lot of programming languages have libraries to parse JSON either built in or community made.

I've also made a quick script that converts the json to an XML format if you'd prefer, using it would however involve connecting to my site as well as the toribash site however, unless you use the source at the bottom of the post. http://dump.wasteofspace.org/xmlstat...s&key=username

A quick demo in PHP

In PHP all we need to do is use the json_decode function to break down a json string into an array, like so:

php code:
<?php
$str = '{"key":300, "name":"Your mum"}';
$json = json_decode($str);
var_dump($json);
//Output:
//object(stdClass)#1 (2) { ["key"]=> int(300) ["name"]=> string(8) "Your mum" }
?>


Now in php to get the contents of a file we can use file_get_contents, like so:php code:
<?php
$str = file_get_contents('http://forum.toribash.com/tori_stats.php?format=json&username=Blam');
$json = json_decode($str);
var_dump($json);
?>


Now to access a single piece of data in our $json table, we go like this:
php code:
<?php

$str = file_get_contents('http://forum.toribash.com/tori_stats.php?format=json&username=Blam');

$json = json_decode($str);

echo $json->{'userid'};

?>





By using the _GET array to access the GET data (the stuff you put in the url, like "?user=Blam&age=17") and a foreach loop we can show all the stats of anyone!
php code:
<?php
$name = $_GET["user"];
$url = "http://forum.toribash.com/tori_stats.php?username=$name&format=json";
$str = file_get_contents($url);
$json = json_decode($str);

foreach($json as $key => $value)
{
echo "$key => $value<br/>";
}
?>


An example of this exact script can be seen here

The source of my simplistic toribash stats JSON to XML:php code:
<?php

// ------------------------------------------------------------------ //
// Creates an xml file containing the users Toribash stats. //
// By Blam //
// ------------------------------------------------------------------- //

header("Content-type:text/xml");

$names = explode(',', $_GET["user"]);
$xmlkey = $_GET["key"];

if(isset($xmlkey) == false)
{
$xmlkey = 'userid';
}

echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><stats>";

foreach($names as $name)
{
$url = "http://forum.toribash.com/tori_stats.php?username=$name&format=json";
if(($str = file_get_contents($url)) !== false)
{
$json = json_decode($str);

echo "<player $xmlkey=\"".$json->{$xmlkey}.'">';

foreach($json as $key => $value)
{
if($key != $xmlkey) echo "<$key>$value</$key>";
}

echo '</player>';
}
}

echo '</stats>';
?>
:D
Yeah I know, but I only need the numbers for a script. JSON format is easy to use and lets not to parse the whole stats page for needed info.