Home / Field guides / First steps

First steps · Field guide · Ad

How to save your first web page

Before checking stock, learn to collect one page you recognise. You will ask ScraperAPI for a copy, save it and look for the words you expected.

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

What you are making

A request is a message from your program asking for a page. In this exercise the address is example.com. You should receive a page headed “Example Domain”.

You can read the steps without an account. To run the optional example, you need a ScraperAPI key and PHP with the cURL extension on your own computer or server.

Choose a page you recognise

Open example.com and look at its heading. Knowing the answer in advance gives you a simple way to tell whether your first request worked. For a real project, choose a page you own or have permission to collect.

Ask ScraperAPI for the page

Your program sends the page address to ScraperAPI. It also sends a private account key so ScraperAPI knows whose request this is. ScraperAPI returns the page contents to your program. The optional code below saves them in a file called sample.html.

Look inside the copy

Open sample.html and look for “Example Domain”. Finding those words tells you the expected page arrived. If you see a sign-in message instead, stop and investigate before reading any stock or prices from it.

Try the same check on a product

Choose one item and write down its code, country and ready-to-ship quantity by hand. Collect its page and look for those same details in the saved copy. You can reread the copy while you adjust the program, without requesting the page again.

Optional: run the code yourself Toggle example

Before starting: PHP is the programming language in this example. cURL is the PHP extension that sends the web request. Run these commands in a private folder on your own computer or server. The key belongs there, away from visitors to your site.

Run the Bash commands in the same terminal session. Type your key at the silent prompt.

Bash
mkdir -p atlas-lab && cd atlas-lab
umask 077
read -rs -p "ScraperAPI key: " SCRAPERAPI_KEY; echo
export SCRAPERAPI_KEY
php -v
php -m | grep -i curl

Save the PHP below as first-request.php in the same private folder. The sample uses PHP 8+ with cURL.

PHP · first-request.php
<?php
// Run locally with PHP 8+ and the cURL extension.
$key = getenv('SCRAPERAPI_KEY');
if ($key === false || trim($key) === '') {
    throw new RuntimeException('Set SCRAPERAPI_KEY before running.');
}

$target = 'https://example.com/';
$query = http_build_query([
    'api_key' => $key,
    'url' => $target,
    'max_cost' => 1,
], '', '&', PHP_QUERY_RFC3986);
$request = curl_init('https://api.scraperapi.com/?' . $query);
curl_setopt_array($request, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 75,
]);
$html = curl_exec($request);
$status = curl_getinfo($request, CURLINFO_RESPONSE_CODE);
if ($html === false || $status !== 200) {
    throw new RuntimeException("Request failed (HTTP $status).");
}
if (stripos($html, 'Example Domain') === false) {
    throw new RuntimeException('Expected page marker is missing.');
}
if (file_put_contents(__DIR__ . '/sample.html', $html) === false) {
    throw new RuntimeException('Could not save the response.');
}
echo 'Verified sample saved.' . PHP_EOL;

Run the script, then inspect the beginning of the saved page.

Bash
php first-request.php
head -n 12 sample.html

Further reading · ScraperAPI’s guide to collecting a page