Example Graph Report call with PHP
Graph Export Example with PHP
In order to download a report via the API, a report must first be saved in the frontend (see requirements).
Basically, two types of graph reports are distinguished. Either runtime-based or accounting-based data is provided. The runtime-based report can be much larger than the accounting-based report.
In the goTom frontend, in the "Haupt Filter" under "Zeitraum", the "Zeitspanne" can be set to "Laufzeit" or another option. If "Laufzeit" is used, the API will automatically be provided with a runtime based report.
<?php
$savedReportId = 18; // replace with the saved report id you have
$host = 'https://example.com'; // Replace with your gotom domain
$reportType = 'graph-export'; // for custom report use 'custom-report-export'
$uri = '/app-api/'.$reportType.'/download/' . $savedReportId;
$provider = 'gotom_app_api';
$user = 'your_user_you_request_via_contact';
$secret = 'your_secret_key_you_request_via_contact';
$args = getopt('', ['echo']);
$echo = isset($args['echo']);
$date = (new DateTime())->format('c');
$method = 'GET';
$content = '';
$contentType = 'text/csv';
$customHeaders = '';
$digestParts = [
strtoupper($method),
md5($content),
strtolower($contentType),
$date,
$customHeaders,
$uri,
];
$digest = hash_hmac('sha1', implode("\n", $digestParts), $secret, true);
$signature = base64_encode($digest);
$headers = [
'Authorization: '.$provider.' '.$user.':'.$signature,
'Content-Type: '.$contentType,
'Date: '.$date,
];
$options = [
'http' => [
'method' => $method,
'header' => implode("\r\n", $headers),
'content' => $content,
'ignore_errors' => true,
],
];
$context = stream_context_create($options);
$bufferLength = 1024 ** 2 * 8; // 8 Mebibyte
file_put_contents(__DIR__.'/response.csv', '');
$handle = fopen($host.$uri, 'rb', null, $context);
while (!feof($handle)) {
$buffer = fread($handle, $bufferLength);
file_put_contents(__DIR__.'/response.csv', $buffer, FILE_APPEND);
if ($echo) {
echo $buffer;
}
}
More related examples
See more code examples over here
Reference of relevant php functions
The script was successfully tested with PHP 7.2.
The following references are useful for other programming languages:
strtoupper = text becomes uppercase http://php.net/manual/en/function.strtoupper.php
strtolower = text becomes lowercase http://php.net/manual/en/function.strtolower.php
md5 = MD5 hash of a string http://php.net/manual/en/function.md5.php
hash_hmac = hash a string by specifying an algorithm http://php.net/manual/en/function.hash-hmac.php
implode = Join an array to a string http://php.net/manual/en/function.implode.php
base64_encode = encode a string in base64 http://php.net/manual/en/function.base64-encode.php
stream_context_create = Create a context for the request http://php.net/manual/en/function.stream-context-create.php
file_put_contents = Write data to a file http://php.net/manual/en/function.file-put-contents.php
fopen = Open handle to a resource http://php.net/manual/en/function.fopen.php
feof = indicate if the resource has reached its end http://php.net/manual/en/function.feof.php
fread = Read the resource http://php.net/manual/en/function.fread.php
Updated 16 days ago