Two basic API tutorials

Updated 3 months ago ​by Geoffroy "Vomact" Piot

Since we announced our API initiative, we’ve seen cool projects from seasoned developers. On the other hand, we also received numerous questions on a more basic level.

Here are some very simple tutorials to start using the API with just PHP and seize its possibilities!

image

Retrieve your tournaments data

This function is designed to help you retrieve data from tournaments you created or have admin permissions on. More info here:

https://developer.toornament.com/doc/tournaments#get:metournaments

As this function requires an “Authorized Access”, you’ll need to provide both your API Key and an Access Token to use it. More info here:

https://developer.toornament.com/security/oauth2

  1. Request an Access Token with the following PHP code

  2. // 1. Getting an access token
    curl_setopt_array(
        $curl, array(
        CURLOPT_URL             => 'https://api.toornament.com/oauth/v2/token?grant_type=client_credentials&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}',
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_VERBOSE         => true,
        CURLOPT_HEADER          => true,
        CURLOPT_SSL_VERIFYPEER  => false,
        CURLOPT_HTTPHEADER      => array(
            'X-Api-Key: {API_KEY}',
            'Content-Type: application/json'
        )
    ));
    $output         = curl_exec($curl);
    $header_size    = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    $header         = substr($output, 0, $header_size);
    $body           = json_decode(substr($output, $header_size));
    $access_token   = $body->access_token;

    Edit the following values with the ones from your application you previously created in our developer website:

    • {CLIENT_ID}
    • {CLIENT_SECRET}
    • {API_KEY}
  3. Retrieve your tournament data thanks to your API Key and your Access Token
  4. // 2. Getting your tournaments
    curl_setopt_array(
        $curl, array(
        CURLOPT_URL             => 'https://api.toornament.com/v1/me/tournaments',
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_SSL_VERIFYPEER  => false,
        CURLOPT_HTTPHEADER      => array(
            'X-Api-Key: wDMBFiXrLbSZ5Vg_nun-gMDmYEQuwb1xabDOo6yDyRQ',
            'Authorization: Bearer '.$access_token,
            'Content-Type: application/json'
        )
    ));
    $output         = curl_exec($curl);
    $header_size    = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    $header         = substr($output, 0, $header_size);
    $body           = json_decode(substr($output, $header_size));

    You just have to edit the {API_KEY} value with your own, as the Access Token is passed throught the “$access_token” variable from the first request.

    You don’t need to request a new Access Token for each request, as it is eligible for 24 hours. Please keep it at hand if you need to make several requests in a row.

Update a match result

We will here use this function from the Toornament API:

https://developer.toornament.com/doc/matches#put:tournaments:tournament_id:matches:id:result

This is a PUT type function.

First, let’s use a “$data” variable containing the result we want to set. For our example, opponent 1 won the match with a 16-12 score.

Here’s the PHP code to enter:

// 3. Setting a match result
$data = '{
    "status": "completed",
    "opponents": [
        {
            "number": 1,
            "result": 1,
            "score": 16,
            "forfeit": false
        },
        {
            "number": 2,
            "result": 3,
            "score": 12,
            "forfeit": false
        }
    ]
}';
curl_setopt_array(
    $curl, array(
    CURLOPT_URL             => 'https://api.toornament.com/v1/tournaments/{TOURNAMENT_ID}/matches/{MATCH_ID}/result',
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_HTTPHEADER      => array(
        'X-Api-Key: {API_KEY}',
        'Authorization: Bearer '.$access_token,
        'Content-Type: application/json'
    ),
    CURLOPT_CUSTOMREQUEST   => 'PUT',
    CURLOPT_POSTFIELDS      => $data
));
$output         = curl_exec($curl);
$header_size    = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header         = substr($output, 0, $header_size);
$body           = json_decode(substr($output, $header_size));

Now send your request.

Replace the following values with your own (remember, we’re still using the same Access Token on “$access_token”):

  • {TOURNAMENT_ID}
  • {MATCH_ID}
  • {API_KEY}

There you have it, two basic functions (retrieve tournament data and update a match result) using our API!

Now for more

Hope you enjoy and dive into all the other possibilities of our API. Get the complete user guide on our dedicated website:

https://developer.toornament.com

In the meantime, here’s the code used for the two function, completed with the end cession function CURL:

<?php
$curl = curl_init();
// 1. Getting an access token
curl_setopt_array(
    $curl, array(
    CURLOPT_URL             => 'https://api.toornament.com/oauth/v2/token?grant_type=client_credentials&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}',
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_VERBOSE         => true,
    CURLOPT_HEADER          => true,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_HTTPHEADER      => array(
        'X-Api-Key: {API_KEY}',
        'Content-Type: application/json'
    )
));
$output         = curl_exec($curl);
$header_size    = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header         = substr($output, 0, $header_size);
$body           = json_decode(substr($output, $header_size));
$access_token   = $body->access_token;
// 2. Getting your tournaments
curl_setopt_array(
    $curl, array(
    CURLOPT_URL             => 'https://api.toornament.com/v1/me/tournaments/',
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_HTTPHEADER      => array(
        'X-Api-Key: {API_KEY}',
        'Authorization: Bearer '.$access_token,
        'Content-Type: application/json'
    )
));
$output         = curl_exec($curl);
$header_size    = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header         = substr($output, 0, $header_size);
$body           = json_decode(substr($output, $header_size));
var_dump($body);
// 3. Setting a match result
$data = '{
    "status": "completed",
    "opponents": [
        {
            "number": 1,
            "result": 1,
            "score": 16,
            "forfeit": false
        },
        {
            "number": 2,
            "result": 3,
            "score": 12,
            "forfeit": false
        }
    ]
}';
curl_setopt_array(
    $curl, array(
    CURLOPT_URL             => 'https://api.toornament.com/v1/tournaments/{TOURNAMENT_ID}/matches/{MATCH_ID}/result',
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_HTTPHEADER      => array(
        'X-Api-Key: {API_KEY}',
        'Authorization: Bearer '.$access_token,
        'Content-Type: application/json'
    ),
    CURLOPT_CUSTOMREQUEST   => 'PUT',
    CURLOPT_POSTFIELDS      => $data
));
$output         = curl_exec($curl);
$header_size    = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header         = substr($output, 0, $header_size);
$body           = json_decode(substr($output, $header_size));
var_dump($body);
// Close request to clear up some resources
curl_close($curl);

How did we do?