Home / Field guides / Recurring checks

Recurring checks · Field guide · Ad

How does a shop collect a page that takes a while?

ScraperAPI can work on a request in the background. Your program can leave, come back later and collect the answer using the same receipt.

Includes a ScraperAPI affiliate link. We earn commission on qualifying purchases.

Why come back later?

A shop might run its stock checker every five minutes. A supplier page might still be loading when the first run ends.

ScraperAPI gives the request an identifying number, called a job ID. Saving it lets the shop check the existing request next time instead of paying to start the work again.

Ask once and keep the job ID

Give ScraperAPI the web address you want collected. Its first reply contains the job ID. At this point the provider has accepted the work; the supplier page may still be loading.

Save the receipt before moving on

Store the ID with the page address and the time you asked. If the connection drops before you receive an ID, mark the request for review. The provider may already have started it.

Use the same ID to get the answer

On the next run, ask for the status of that job. If it is still running, keep the receipt. If it is finished, save the reply promptly and check the page before using its stock or price. Completed results stay with the provider for a limited time.

Practise stopping between the two steps

The optional example has one script for submitting the request and another for collecting it. Run the first once, stop, then run the second. A real shop also records which results it has already used so restarting the checker cannot apply the same result twice.

Optional: run the code yourself Toggle example

Set SCRAPERAPI_KEY using the first guide. Save as submit.php in a private directory and run it once. This example uses PHP 8+ with cURL.

PHP · submit.php
<?php
// Run once with PHP 8+ and cURL. Use a private working directory.
$key = getenv('SCRAPERAPI_KEY');
if ($key === false || trim($key) === '') {
    throw new RuntimeException('Set SCRAPERAPI_KEY first.');
}
$receiptPath = __DIR__ . '/job.json';
// Exclusive create prevents an accidental second paid submission.
$receipt = @fopen($receiptPath, 'x');
if ($receipt === false) {
    throw new RuntimeException('Check job.json before submitting again.');
}
fwrite($receipt, '{"state":"submission-unconfirmed"}');
fflush($receipt);

$ch = curl_init('https://async.scraperapi.com/jobs');
curl_setopt_array($ch, [
    CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
    CURLOPT_TIMEOUT => 75,
    CURLOPT_POSTFIELDS => json_encode([
        'apiKey' => $key, 'url' => 'https://example.com/',
        'apiParams' => ['max_cost' => 1],
    ], JSON_THROW_ON_ERROR),
]);
$raw = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$job = is_string($raw) ? json_decode($raw, true) : null;
$id = is_array($job) ? ($job['id'] ?? null) : null;
if ($http < 200 || $http >= 300 || !is_string($id)
    || !preg_match('/^[a-zA-Z0-9-]+$/D', $id)) {
    throw new RuntimeException('Submission unconfirmed. Inspect before retrying.');
}
rewind($receipt);
ftruncate($receipt, 0);
fwrite($receipt, json_encode(['id' => $id], JSON_THROW_ON_ERROR));
fclose($receipt);
echo 'Job ID saved.' . PHP_EOL;

Save as poll.php in the same private directory. Run it to collect the existing job and inspect the saved result.

PHP · poll.php
<?php
// Run again while status is "running". This script never submits a job.
$job = json_decode(file_get_contents(__DIR__ . '/job.json'), true, 512, JSON_THROW_ON_ERROR);
$id = $job['id'] ?? '';
if (!is_string($id) || !preg_match('/^[a-zA-Z0-9-]+$/D', $id)) {
    throw new RuntimeException('No confirmed job ID to poll.');
}
$ch = curl_init('https://async.scraperapi.com/jobs/' . $id);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 75]);
$raw = curl_exec($ch);
if ($raw === false || curl_getinfo($ch, CURLINFO_RESPONSE_CODE) !== 200) {
    throw new RuntimeException('Poll failed. Keep the job ID for inspection.');
}
$result = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
$status = $result['status'] ?? 'unknown';
if ($status === 'finished') {
    if (file_put_contents(__DIR__ . '/result.json', $raw, LOCK_EX) === false) {
        throw new RuntimeException('Could not save result.');
    }
    echo 'Saved result.json. Validate response.body before importing.' . PHP_EOL;
} else {
    echo 'Job status: ' . $status . PHP_EOL;
}

Further reading · How ScraperAPI handles background jobs