NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

AWS WAF and other CAPTCHAs

How to solve the AWS WAF CAPTCHA in PHP

PHP's cURL extension can carry the AWS WAF cookie for you once it is in the handle's cookie store. This guide shows the whole loop with no Composer packages, at $1.40 per 1,000 solved tasks.

The approach in one paragraph

You keep one cURL handle for the whole session, with a cookie jar file switched on. When a response turns out to be the AWS WAF puzzle, you ask the API for a cookie, push it into the handle's cookie store with CURLOPT_COOKIELIST, and repeat the request. From then on cURL sends aws-waf-token on every request to that host, and the jar file keeps it between runs of the script until it expires.

Everything here uses functions that ship with PHP's cURL extension, so it runs on a shared host, inside a Laravel job or from a cron script alike. PHP 8.1 or newer is assumed for the syntax.

A session handle with a cookie jar

Two options make the jar work. CURLOPT_COOKIEFILE tells cURL to read cookies from the file at startup and enables the cookie engine; CURLOPT_COOKIEJAR tells it where to write them when the handle is released. Point both at the same writable path.

<?php
function wafSession(string $jarPath): CurlHandle
{
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_TIMEOUT        => 25,
        CURLOPT_COOKIEFILE     => $jarPath,
        CURLOPT_COOKIEJAR      => $jarPath,
        CURLOPT_USERAGENT      => 'Mozilla/5.0 (X11; Linux x86_64) stock-watch/2.0',
        CURLOPT_ENCODING       => '',
    ]);
    return $ch;
}

function pageGet(CurlHandle $ch, string $url): array
{
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    $html = (string) curl_exec($ch);
    return [curl_getinfo($ch, CURLINFO_RESPONSE_CODE), $html];
}

An empty string for CURLOPT_ENCODING asks cURL to accept every compression it supports and decode the body for you. Without it some sites send compressed bytes you would have to unpack yourself.

Recognising the WAF response

At the time of writing the CAPTCHA action replies with status 405 and a page that sets a gokuProps object holding key, iv and context. The function below pulls those out with one regular expression and json_decode. If the status is 405 but the object is missing, it still reports a challenge with no extras, because the task works with the URL alone.

function wafProps(int $status, string $html): ?array
{
    if ($status !== 405 && $status !== 202) {
        return null;
    }
    if (!preg_match('/gokuProps\s*=\s*(\{.*?\})/s', $html, $m)) {
        return $status === 405 ? [] : null;
    }
    $p = json_decode($m[1], true) ?: [];
    return array_filter([
        'awsKey'     => $p['key'] ?? null,
        'awsIv'      => $p['iv'] ?? null,
        'awsContext' => $p['context'] ?? null,
    ]);
}

array_filter without a callback removes the null entries, so a missing value never reaches the task as an empty field.

Asking the API for the cookie

The API calls use a second, short lived cURL handle so the target site's cookies never mix with your API traffic. The task type is AntiAwsWafTaskProxyLess; the answer is the string in solution.cookie.

function gsCall(string $method, array $payload): array
{
    $payload['clientKey'] = getenv('GS_SECRET') ?: 'YOUR_API_KEY';
    $api = curl_init('https://gatesolver.com/' . $method);
    curl_setopt_array($api, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Accept: application/json'],
        CURLOPT_POSTFIELDS => json_encode($payload),
        CURLOPT_TIMEOUT => 30,
    ]);
    $out = json_decode((string) curl_exec($api), true) ?? [];
    if (!empty($out['errorId'])) {
        throw new RuntimeException($out['errorCode'] ?? 'API error');
    }
    return $out;
}

function solveWaf(string $url, array $extras): string
{
    $task = ['type' => 'AntiAwsWafTaskProxyLess', 'websiteURL' => $url] + $extras;
    $id = gsCall('createTask', ['task' => $task])['taskId'];
    $deadline = time() + 125;
    while (time() < $deadline) {
        sleep(3);
        $r = gsCall('getTaskResult', ['taskId' => $id]);
        $state = $r['status'] ?? '';
        if ($state === 'ready') {
            return $r['solution']['cookie'];
        }
        if ($state === 'failed') {
            break;
        }
    }
    throw new RuntimeException("AWS WAF task $id gave no cookie");
}

Putting the cookie into the jar

libcurl accepts a cookie in the same text form a server would send it, prefixed with Set-Cookie:, through CURLOPT_COOKIELIST. Giving it a domain and path scopes it like a cookie the site set itself.

function fetchPastWaf(CurlHandle $ch, string $url): string
{
    [$code, $html] = pageGet($ch, $url);
    $extras = wafProps($code, $html);
    if ($extras === null) {
        return $html;
    }
    $cookie = solveWaf($url, $extras);
    $host = parse_url($url, PHP_URL_HOST);
    curl_setopt($ch, CURLOPT_COOKIELIST,
        "Set-Cookie: aws-waf-token=$cookie; domain=$host; path=/; secure");
    [$code, $html] = pageGet($ch, $url);
    if (wafProps($code, $html) !== null) {
        throw new RuntimeException('still blocked; try the proxy task');
    }
    return $html;
}

$session = wafSession(__DIR__ . '/waf-cookies.txt');
echo strlen(fetchPastWaf($session, 'https://example.com/offers?page=1')), PHP_EOL;
unset($session);

The last line releases the handle, which is the moment cURL writes the jar to disk. Since PHP 8.0, curl_close() no longer frees the handle by itself, so dropping the last reference (or letting the script end) is what triggers the write. On the next run, CURLOPT_COOKIEFILE loads the token again and the first request may pass without a new solve.

Proxy tasks and other details

If the repeat request still gets the puzzle, the site probably expects the cookie from the same IP that solved it. Set CURLOPT_PROXY on the session handle and send AntiAwsWafTask with a proxy field holding the same address as http://user:pass@host:port. The proxy format note lists the accepted forms.

A few more points that save debugging time:

  • Keep the jar file outside the web root; it contains session cookies for the target site.
  • Run only one process per jar file. Two scripts writing the same file can overwrite each other's cookies.
  • Use curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) after redirects to send the real puzzle URL to the task when the site redirects before blocking.
  • Read the gokuProps values from the response you are answering. The context value is not meant to be reused.

Knowing when to solve again

The cookie does not announce its own expiry in a way your script can rely on, because the immunity period is a setting on the site's side. The simple and dependable rule is to look at every response: when wafProps() returns something other than null, the token has run out or was never accepted, and fetchPastWaf() earns a new one. Wrap all page requests in that function and the script heals itself on long runs.

It also helps to log one line per solve with the time, the URL and the task id. After a day of runs you can see how long a token lasts on that site and space your crawl accordingly; if a cookie survives fifteen minutes, there is little point fetching the same listing every thirty seconds. That keeps cost down and puts less load on the site, which in turn makes the firewall less likely to step in at all.

Handling API errors

ERROR_ZERO_BALANCE should stop the job and alert you; topping up from $40 restores service, and the balance never expires. ERROR_CAPTCHA_UNSOLVABLE and ERROR_SERVICE_UNAVAILABLE are free and can be retried after a pause. ERROR_KEY_DENIED_ACCESS usually means the API key was regenerated in the account and the old one in your environment is gone. The full list is in the API documentation.

PHP cron jobs that refresh price lists or check stock on public pages are a good fit for this pattern. Solve once per run, keep the jar, and let the cookie carry the rest of the requests.

For the same flow in other stacks, see the Python guide, or compare with how the reCAPTCHA v2 PHP guide submits a token in a form.

Common questions

Do I need Guzzle or another library for this?

No. Everything uses the cURL extension that ships with PHP, which keeps the script portable to shared hosting and cron.

Why use a separate cURL handle for the API calls?

It keeps the target site's cookie jar free of API traffic and lets each handle keep its own timeouts and headers.

When is the cookie jar file written?

When the cURL handle is released. Unset the variable or let the script end; on PHP 8 curl_close alone does not free it.

Can I put the cookie in a header instead of the jar?

Yes, CURLOPT_COOKIE can send it on one request, but the jar keeps it for every later request and between script runs.

Does the in.php interface support AWS WAF?

No. AWS WAF tasks are available only through createTask and getTaskResult with JSON, as shown on this page.

Keep reading