PHP code example of daktela / daktela-v6-php-connector
1. Go to this page and download the library: Download daktela/daktela-v6-php-connector library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
daktela / daktela-v6-php-connector example snippets
use Daktela\DaktelaV6\Client;
$client = new Client($instance, $accessToken);
$client->getApiCommunicator()->setRequestTimeout(30.0); // 30 seconds
use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\RequestFactory;
$instance = "https://mydaktela.daktela.com/";
$accessToken = "your-access-token";
$client = new Client($instance, $accessToken);
$request = RequestFactory::buildReadRequest("Users")
->addFilter("username", "eq", "admin");
$response = $client->execute($request);
use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\Http\ApiCommunicator;
$communicator = new ApiCommunicator($instance, $accessToken);
$communicator->setRequestTimeout(10.0);
$client = new Client($instance, $accessToken, $communicator);
use Daktela\DaktelaV6\Exception\NotFoundException;
use Daktela\DaktelaV6\Exception\RequestException;
try {
$response = $client->execute($request);
} catch (NotFoundException $ex) {
// The object does not exist
} catch (RequestException $ex) {
$status = $ex->getHttpStatus(); // e.g. 400, or null for transport failures
$errors = $ex->getApiErrors(); // the `error` member of the API response
$body = $ex->getResponseBody(); // raw response body
}
use Daktela\DaktelaV6\Client;
$client = Client::login($instance, $username, $password);
use Daktela\DaktelaV6\Auth\TokenSet;
use Daktela\DaktelaV6\Auth\TokenStore;
use Daktela\DaktelaV6\Client;
final class FileTokenStore implements TokenStore
{
public function __construct(private string $path)
{
}
public function load(): ?TokenSet
{
$data = is_file($this->path) ? json_decode((string)file_get_contents($this->path), true) : null;
return is_array($data) ? TokenSet::fromArray($data) : null;
}
public function save(TokenSet $tokens): void
{
// tempnam() creates the file readable only by its owner; rename() replaces the old file atomically
$file = tempnam(dirname($this->path), 'daktela');
file_put_contents($file, json_encode($tokens->toArray()));
rename($file, $this->path);
}
}
$client = Client::login($instance, $username, $password, new FileTokenStore('/var/lib/myapp/daktela-tokens.json'));
use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\Http\ApiCommunicator;
$communicator = new ApiCommunicator($instance, '');
$communicator->setRequestTimeout(30.0);
$communicator->login($username, $password);
$client = new Client($instance, '', $communicator);
use Daktela\DaktelaV6\Client;
$client = new Client($instance, $accessToken);
// Token is automatically sent via the Authorization: Bearer header
use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\Http\ApiCommunicator;
$client = new Client($instance, $accessToken);
$client->getApiCommunicator()->setAuthenticationMethod(
ApiCommunicator::AUTHENTICATION_METHOD_HEADER
);
use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\Http\ApiCommunicator;
$client = new Client($instance, $accessToken);
$client->getApiCommunicator()->setAuthenticationMethod(
ApiCommunicator::AUTHENTICATION_METHOD_QUERY
);
use Psr\Log\LoggerInterface;
// Using any PSR-3 logger (Monolog, etc.)
$client->getApiCommunicator()->setLogger($logger);
use GuzzleHttp\Client as GuzzleClient;
$httpClient = new GuzzleClient([
'base_uri' => 'https://your-instance.daktela.com',
'proxy' => 'http://proxy.example.com:8080',
'timeout' => 60,
]);
$client->getApiCommunicator()->setHttpClient($httpClient);
use Daktela\DaktelaV6\Http\RetryConfig;
$client->getApiCommunicator()->setRetryConfig(new RetryConfig(
maxRetries: 3, // Number of retry attempts
baseDelayMs: 100, // Initial delay in milliseconds
maxDelayMs: 10000, // Maximum delay between retries
multiplier: 2.0, // Exponential backoff multiplier
retryableStatusCodes: [500, 502, 503, 504],
retryOnConnectionError: true,
retryNonIdempotentRequests: false // Do not repeat POST after timeouts/5xx
));
// Quick presets
$client->getApiCommunicator()->setRetryConfig(RetryConfig::aggressive()); // 5 retries
$client->getApiCommunicator()->setRetryConfig(RetryConfig::disabled()); // No retries
use Daktela\DaktelaV6\Http\RateLimitConfig;
$client->getApiCommunicator()->setRateLimitConfig(new RateLimitConfig(
autoRetry: true, // Automatically wait and retry
maxWaitSeconds: 60, // Maximum time to wait
defaultWaitSeconds: 5 // Default wait if Retry-After header missing
));
// Simple ping
if ($client->ping()) {
echo "API is reachable and accepts the access token";
}
// Detailed health check
$health = $client->healthCheck();
// Healthy: ['healthy' => true, 'latency_ms' => 45.2, 'status_code' => 200]
// Token rejected: ['healthy' => false, 'latency_ms' => 40.1, 'status_code' => 200, 'error' => 'The access token was not accepted: the API did not identify a user.']
// API error: ['healthy' => false, 'latency_ms' => 52.7, 'status_code' => 400, 'error' => 'New password should be set']
// Network failure: ['healthy' => false, 'latency_ms' => 2001.3, 'error' => 'cURL error 7: Failed to connect ...']
$request = RequestFactory::buildReadRequest("CampaignsRecords")
->addFilter("created", "gte", "2020-01-01 00:00:00");
// Iterate over all records
foreach ($client->iterate($request) as $record) {
echo $record->name;
}
// With options
$iterator = $client->iterate(
$request,
pageSize: 100, // Records per API call
maxItems: 1000, // Stop after 1000 items
stopOnError: true // Stop on first error
);
// Helper methods
$first = $iterator->first(); // Get first item
$all = $iterator->toArray(); // Collect all to array
$count = $iterator->count(); // Count all items
$isEmpty = $iterator->isEmpty(); // Check if empty
// Functional operations
$iterator->each(fn($item) => process($item));
$filtered = $iterator->filter(fn($item) => $item->active);
$mapped = $iterator->map(fn($item) => $item->name);
// Iterate over pages (for access to total counts)
foreach ($iterator->pages() as $response) {
echo "Total: " . $response->getTotal();
foreach ($response->getData() as $item) {
// Process item
}
}
$iterator = $client->iterate($request);
foreach ($iterator as $record) {
// ...
}
if ($iterator->hasStoppedOnError()) {
$errors = $iterator->getErrorResponse()->getErrors();
}
$response = $client->execute($request);
// Check success (HTTP 2xx)
if ($response->isSuccess()) {
$data = $response->getData();
}
// Check for errors
if ($response->hasErrors()) {
// The first entry of the errors, which the API reports as a list or as a map such as ['title' => '
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.