Trackmania Nations : API use
How to use the API

The TMN ESWC is designed to be easy to use with php4 or php5.
The API system embed a cache system of 24 hours, thus, it is not necessary to update your datas each minutes.

PHP4 use

php4 is not enable to transform xml natively. For this reason, the function xml2array allow you to transform xml datas to a simple array.

Here is a complete example of php4 use: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>ladder fetching exemple</title>

<style type="text/css">
    body { font-family:Arial; }
    .exemple { border:1px solid gray; border-collapse:collapse;}
    .exemple td {border:1px solid gray; padding: 0px 10px; text-align:center;}
</style>
</head>

<body>
<?php
include('xml2array.php'); // include the xml to array function

// We choose the url. here the world ladder from the rank 50 to 53 (4 players)
$url "http://tmladder.free.fr/request.php?";
$params "game=tmn&type=ranking&start=50&limit=4";

// We get ladder datas
$xml file_get_contents($url.$params);

// Conversion of the xml to an array
$resultat xml2array($xml);

// With those 3 lines, you can see what contains exactly the array
// you can un-comment them during your tests
//echo "<pre>";
//print_r($resultat);
//echo "</pre>";

//table display exemple
echo '<table class="exemple">';
foreach(
$resultat['player'] as $a)
    {
    
//small processing on nicknames (not directly readable)
    
$nick urldecode($a['nick']['value']);
    
    echo 
'<tr>';
    echo 
'<td>'.$a['rank']['value'].'</td>';
    echo 
'<td>'.$a['login']['value'].'</td>';
    echo 
'<td>'.$nick.'</td>';
    echo 
'<td>'.$a['nationality']['value'].'</td>';
    echo 
'<td>'.$a['score']['value'].' pts</td>';
    echo 
'</tr>';
    }
echo 
'</table>';

?>
</body>

Result of this exemple:

Content of the file xml2array.php:

<?php
function xml2array($contents$get_attributes=1) {
    if(!
$contents) return array();
    if(!
function_exists('xml_parser_create')) { return array(); }
    
$parser xml_parser_create();
    
xml_parser_set_option$parserXML_OPTION_CASE_FOLDING);
    
xml_parser_set_option$parserXML_OPTION_SKIP_WHITE);
    
xml_parse_into_struct$parser$contents$xml_values );
    
xml_parser_free$parser );
    if(!
$xml_values) return;
    
$xml_array = array();
    
$parents = array();
    
$opened_tags = array();
    
$arr = array();
    
$current = &$xml_array;
    foreach(
$xml_values as $data) {
        unset(
$attributes,$value);
        
extract($data);
        
$result '';
        if(
$get_attributes) {
            
$result = array();
            if(isset(
$value)) $result['value'] = $value;
            if(isset(
$attributes)) {
                foreach(
$attributes as $attr => $val) {
                    if(
$get_attributes == 1$result['attr'][$attr] = $val;
                }
            }
        } elseif(isset(
$value)) {
            
$result $value;
        }
        if(
$type == "open") {
            
$parent[$level-1] = &$current;
            if(!
is_array($current) or (!in_array($tagarray_keys($current)))) { 
                
$current[$tag] = $result;
                
$current = &$current[$tag];
            } else { 
                if(isset(
$current[$tag][0])) {
                    
array_push($current[$tag], $result);
                } else {
                    
$current[$tag] = array($current[$tag],$result);
                }
                
$last count($current[$tag]) - 1;
                
$current = &$current[$tag][$last];
            }
        } elseif(
$type == "complete") {
            if(!isset(
$current[$tag])) {
                
$current[$tag] = $result;
            } else {
                if((
is_array($current[$tag]) and $get_attributes == 0)
                        or (isset(
$current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) {
                    
array_push($current[$tag],$result); 
                } else { 
                    
$current[$tag] = array($current[$tag],$result); 
                }
            }
        } elseif(
$type == 'close') { 
            
$current = &$parent[$level-1];
        }
    }
    
$keys array_keys($xml_array);
    
$tRetour $xml_array[$keys[0]];
    return(
$tRetour);
}
PHP5 use

Here is a complete example of php5 use:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>ladder fetching exemple</title>

<style type="text/css">
    body { font-family:Arial; }
    .exemple { border:1px solid gray; border-collapse:collapse;}
    .exemple td {border:1px solid gray; padding: 0px 10px; text-align:center;}
</style>
</head>

<body>
<?php
include('xml2array.php'); // include the xml to array function

// We choose the url. here the world ladder from the rank 50 to 53 (4 players)
$url "http://tmladder.free.fr/request.php?";
$params "game=tmn&type=ranking&start=50&limit=4";

// We get ladder datas
$xml file_get_contents($url.$params);

// Conversion of the xml to an array
$resultat xml2array($xml);

// With those 3 lines, you can see what contains exactly the array
// you can un-comment them during your tests
//echo "<pre>";
//print_r($resultat);
//echo "</pre>";

//table display exemple
echo '<table class="exemple">';
foreach(
$resultat['player'] as $a)
    {
    
//small processing on nicknames (not directly readable)
    
$nick urldecode($a['nick']['value']);
    
    echo 
'<tr>';
    echo 
'<td>'.$a['rank']['value'].'</td>';
    echo 
'<td>'.$a['login']['value'].'</td>';
    echo 
'<td>'.$nick.'</td>';
    echo 
'<td>'.$a['nationality']['value'].'</td>';
    echo 
'<td>'.$a['score']['value'].' pts</td>';
    echo 
'</tr>';
    }
echo 
'</table>';

?>
</body>

Result of this exemple:

TM Forever top 10 Get Trackmania Nations Forever
banner
Quick links TMX tm Carpark
tm-creative tm-forum
© 2008-2009 tm-ladder.free.fr - All rights reserved - v2.0
A site of the tm-ladder group