PHP for Exporting Data from Rossum API

This PHP code shows how to download JSON data captured in a given Rossum queue for the previous calendar day.

This PHP code shows how to download JSON data captured in a given queue for the previous calendar day – as part of the regular export routine. Update the parameters at the beginning based on your scenario. You can simply run the script as, say, php -f export.php.

The export API is more powerful - you can filter by different criteria or download the data e.g. as an XML instead - see the full docs.

<?php

define('ROSSUM_API_ENDPOINT', 'https://api.elis.rossum.ai/v1/');

$username = 'YOUR USERNAME';
$password = 'YOUR PASSWORD';
$queue_id = 12345;

$date_start = date('Y-m-d', strtotime('-2 day'));
$date_end = date('Y-m-d', strtotime('-1 day'));

function getLoginToken($username, $password, $verbose = false) {
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => ROSSUM_API_ENDPOINT . 'auth/login',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode(array(
            'username' => $username,
            'password' => $password,
        )),
        CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
        CURLOPT_VERBOSE => $verbose,
    ));
    $raw_response = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($raw_response, true);

    if (!isset($response['key'])) {
        throw new Exception('Cannot obtain login token. Message: ' . $raw_response);
    }

    return $response['key'];
}

function makeRequest($url, $token, $verbose = false) {
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Authorization: token ' . $token,
        ),
        CURLOPT_VERBOSE => $verbose,
    ));
    $raw_response = curl_exec($ch);
    curl_close($ch);
    return json_decode($raw_response);
}

function constructUrl($queue_id, $date_start, $date_end) {
    $queryParams = array(
        'format' => 'json',
        'exported_at_after' => $date_start,
        'exported_at_before' => $date_end,
        'page_size' => '100',
    );
    return ROSSUM_API_ENDPOINT . 'queues/' . $queue_id . '/export?' . http_build_query($queryParams);
}


$token = getLoginToken($username, $password);
echo 'Obtained login token: ' . $token . "\n";

$url = constructUrl($queue_id, $date_start, $date_end);
$i = 0;

while ($url) {
    $response = makeRequest($url, $token);
    $i++;

    echo 'Downloaded page ' . $i . ' of ' . $response->pagination->total_pages . "\n";

    $annotations = $response->results;
    $url = $response->pagination->next;

    // TODO: Your code for processing annotations
    var_dump($annotations);
}

The code is a little complex at the end since it handles pagination - if there is more than 100 entries to be exported in the range, the code issues additional export calls to download the remaining ones. (We ignore this complexity in most other examples, but show it in this case.)