Skip to content

About

Apify API client for PHP—Programmatically run Actors, manage and stream data from storages (datasets, key-value stores, request queues), schedule and monitor runs, and access the full Apify platform API. Sync and async interfaces with automatic retries and pagination.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Repository files navigation

Apify API client for PHP

Official, but experimental. This client is generated and maintained by AI. Review the code before relying on it in production, and report issues on the repository.

A PHP client for the Apify API, modeled on the JavaScript client. Start from an ApifyClient and go on to Actors, runs, datasets, key-value stores, request queues, tasks, schedules, webhooks, the Store, users, and logs.

Requirements

  • PHP 8.1 or newer with the json, mbstring, and hash extensions.

Installation

composer require apify/apify-client

Quick start

The snippets assume that the client types are imported, for example use Apify\Client\ApifyClient;.

$client = new ApifyClient('my-api-token');

// Start an Actor and wait for it to finish. The last argument bounds the wait in seconds,
// null waits indefinitely.
$run = $client->actor('apify/hello-world')->call(null, null, null);

// getDefaultDatasetId() is ?string, so cast it for dataset(string $id).
$items = $client->dataset((string) $run->getDefaultDatasetId())->listItems();
echo 'Item count: ' . $items->getCount() . PHP_EOL;

The client does not read APIFY_TOKEN. Pass it yourself, for example new ApifyClient(getenv('APIFY_TOKEN')). You can find your token in Apify Console → Settings → API & Integrations.

Configuration

The constructor takes named arguments:

$configured = new ApifyClient(
    token: 'my-api-token',
    baseUrl: 'https://api.apify.com',
    maxRetries: 5,
    minDelayBetweenRetriesMillis: 1000,
    timeoutSecs: 120,
    userAgentSuffix: 'my-app/1.2.3',
);
Argument Default Meaning
token null API token, sent as a Bearer token.
baseUrl https://api.apify.com API base URL, with or without /v2.
publicBaseUrl baseUrl Base URL of public resource URLs, with or without /v2.
maxRetries 8 Maximum retries of a failed request.
minDelayBetweenRetriesMillis 500 Minimum delay between retries, which grows exponentially.
maxDelayBetweenRetriesMillis timeoutSecs × 1000 Maximum delay between retries.
timeoutSecs 360 Default timeout of the long tier and of timeoutMaxSecs.
timeoutShortSecs 5 Timeout of the short tier, see Timeout tiers.
timeoutMediumSecs 30 Timeout of the medium tier.
timeoutLongSecs timeoutSecs Timeout of the long tier.
timeoutMaxSecs timeoutSecs Cap on the timeout of any attempt, per-call overrides, and growth across retries included.
compression null (brotli, else gzip) Request body compression: 'brotli', 'gzip', 'zstd', or a custom HttpCompressorInterface, see HTTP compression.
userAgentSuffix null Suffix appended to the User-Agent header.
httpClient Guzzle The transport, an Apify\Client\Http\HttpClientInterface.

Requests are retried with exponential backoff and jitter on network errors, HTTP 429, and 5xx. Other 4xx responses throw an ApifyApiException subclass for the status. A 404 on a single resource is not thrown: get() returns null and delete() does nothing, see Error handling.

Timeout tiers

Each method has a timeout tier: short for metadata reads and writes such as get(), update() and delete(), medium for listing, batch, and trigger calls, and long for downloads, uploads, and streaming. The tiers last 5 seconds, 30 seconds, and timeoutSecs (360 by default). Set timeoutShortSecs, timeoutMediumSecs or timeoutLongSecs to change one.

A call that asks the API to hold its response with waitForFinish (RunClient::get(), BuildClient::get(), ActorClient::defaultBuild(), and the waitForFinish option of ActorClient::start(), build() and TaskClient::start()) gets that hold on top of its tier, for at most 60 seconds.

Every request method also takes a trailing $timeoutSecs that overrides the tier for that call: seconds, a tier name ('short', 'medium', 'long'), or 'noTimeout':

$client->actor('my-actor')->get(timeoutSecs: 10); // 10 seconds
$client->dataset('my-dataset')->listItems(timeoutSecs: 'long'); // the 'long' tier
$client->requestQueue('my-queue')->unlockRequests(timeoutSecs: 'noTimeout'); // no timeout

timeoutMaxSecs caps every tier and override, so raise it for calls that need more than 360 seconds. A retry doubles the timeout up to that cap. The timeoutSecs of a request queue client, set in requestQueue(id, options), caps the calls of that client too.

HTTP compression

A request body of at least 1 KiB is compressed, unless its Content-Type is already compressed (image/*, audio/*, video/*, common archive, office, and font formats) or the request has a Content-Encoding header. By default the client uses brotli when the PECL brotli extension is loaded and gzip (the zlib extension) otherwise. Compression is best-effort: without a codec, the body is sent as it is.

Pass compression to choose an algorithm, or a compressor to also set the quality:

use Apify\Client\Http\BrotliHttpCompressor;
use Apify\Client\Http\GzipHttpCompressor;
use Apify\Client\Http\ZstdHttpCompressor;

$client = new