Skip to content

PHP Example

The example below checks authentication, starts a Worker run, then reads results with the returned run_slug.

YOUR_WORKER_ID is a placeholder. Replace it with a Worker slug, or encode an owner/name path as owner~name. Build input from that Worker’s input schema; fields differ by Worker.

The example uses is_async: true for async submit-and-poll behavior. Set is_async to false when the caller should wait for execution to finish, and use offset / limit to control the synchronous result window.

<?php
$apiBaseUrl = "https://openapi.coreclaw.com";
$apiKey = getenv("CORECLAW_API_KEY");
$workerId = getenv("CORECLAW_WORKER_ID") ?: "YOUR_WORKER_ID";
if (!$apiKey) {
throw new RuntimeException("Set CORECLAW_API_KEY first.");
}
function coreclaw_request(string $method, string $path, ?array $query = null, ?array $body = null): array
{
global $apiBaseUrl, $apiKey;
$url = $apiBaseUrl . $path;
if ($query) {
$url .= "?" . http_build_query($query);
}
$headers = ["Authorization: Bearer " . $apiKey];
$options = [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
];
if ($body !== null) {
$headers[] = "Content-Type: application/json";
$options[CURLOPT_HTTPHEADER] = $headers;
$options[CURLOPT_POSTFIELDS] = json_encode($body, JSON_UNESCAPED_SLASHES);
}
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$raw = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($raw === false || $status < 200 || $status >= 300) {
throw new RuntimeException("HTTP " . $status . ": " . $raw);
}
$payload = json_decode($raw, true);
if (($payload["code"] ?? null) !== 0) {
throw new RuntimeException($raw);
}
return $payload;
}
$account = coreclaw_request("GET", "/api/v2/users/account");
print_r($account["data"]);
$run = coreclaw_request("POST", "/api/v2/workers/" . rawurlencode($workerId) . "/runs", null, [
// Replace input.parameters.custom with fields from the Worker's input schema.
"input" => [
"parameters" => [
"custom" => [
"keywords" => ["coffee"],
"base_location" => "New York,USA",
"max_results" => 1,
],
],
],
"is_async" => true,
"offset" => 0,
"limit" => 20,
]);
$runId = $run["data"]["run_slug"];
echo "Run ID: " . $runId . PHP_EOL;
$results = coreclaw_request("GET", "/api/v2/worker-runs/" . rawurlencode($runId) . "/result", [
"offset" => 0,
"limit" => 20,
]);
print_r($results["data"]);