PHP client for the Keboola Query Service API, built on keboola/php-api-client-base.
composer require keboola/query-api-php-client<?php
use Keboola\QueryApi\Client;
$client = new Client(
'https://query.keboola.com', // base URL
'your-storage-api-token', // X-StorageApi-Token
);
// Submit a query job
$response = $client->submitQueryJob('main', 'workspace-123', [
'statements' => ['SELECT * FROM table1'],
'transactional' => true,
]);
$queryJobId = $response->getQueryJobId();
// Poll until it finishes
$status = $client->waitForJobCompletion($queryJobId);
// Read results for the first completed statement
$statementId = $status->getStatements()[0]->getId();
$results = $client->getJobResults($queryJobId, $statementId);
foreach ($results->getData() as $row) {
// ...
}
// Cancel if needed
$client->cancelJob($queryJobId, ['reason' => 'User requested cancellation']);new Client(
string $baseUrl,
string $storageToken,
?string $runId = null, // sent as X-KBC-RunId on every request
?Psr\Log\LoggerInterface $logger = null,
int $backoffMaxTries = 3, // retries on 5xx / transport errors, never 4xx
int $connectTimeout = 10,
int $requestTimeout = 120,
string $userAgent = 'Keboola Query API PHP Client',
null|Closure|GuzzleHttp\HandlerStack $requestHandler = null, // inject a mock handler in tests
);All failures throw Keboola\QueryApi\Exception\ClientException (a subclass of the base client's
exception). It exposes getStatusCode(): ?int and getResponseBody(): ?string; the message is
taken from the API response's exception field when present.
Run inside the library's docker service:
composer install
composer ci # validate + phpcs + phpstan + phpunitFunctional tests (tests/Functional/) require STORAGE_API_TOKEN, HOSTNAME_SUFFIX
(the Query Service URL is https://query.{HOSTNAME_SUFFIX}) and STORAGE_API_URL.
MIT — see LICENSE.