Skip to content

Configuration reference ​

The Scraper constructor is the only place with configuration; parsers have none.

php
public function __construct(array $guzzleOptions = [], ?\GuzzleHttp\ClientInterface $httpClient = null);
ArgumentWithout $httpClientWith $httpClient
$guzzleOptionsConfiguration of the Guzzle client the scraper creates, merged over the defaults with array_merge()Request options sent with every parseUrl() request; the defaults are not used
$httpClientUsed as it is, for every request

Options ​

The options are Guzzle request options with the same meaning. The array is typed (a PHPStan array{…} shape) for these keys:

OptionTypeDefault in the scraper's clientMeaning
connect_timeoutint|float5Seconds to wait for the TCP/TLS connection. 0 waits forever.
timeoutint|floatnone (Guzzle 0: forever)Seconds for the whole request, including the body. Set it.
read_timeoutint|floatPHP default_socket_timeoutSeconds to wait for data on a streamed body.
headersarray<string, string|non-empty-list<string>>['User-Agent' => 'Tomaj\Scraper']Request headers. Replaces the default array: include a User-Agent.
proxystring|array{http?: string, https?: string, no?: list<string>}noneProxy URL, or one per scheme plus hosts that bypass it.
allow_redirectsbool|array{max?: int, strict?: bool, referer?: bool, protocols?: non-empty-list<string>, track_redirects?: bool}Guzzle: up to 5 redirects, http and httpsfalse disables redirects (the 3xx response itself is parsed, usually an empty body).
verifybool|stringtrueTLS certificate verification, or a path to a CA bundle. Do not set false in production.
http_errorsbooltruetrue: a 4xx/5xx response throws FetchException. false: the error page is parsed like any other.
decode_contentbool|stringtrueDecode gzip/deflate responses; a string sets the Accept-Encoding.
force_ip_resolvestringnone'v4' or 'v6' to use only IPv4 or IPv6.
versionstring|float1.1HTTP protocol version.

Other Guzzle options (curl, on_stats, cookies, auth, handler, …) are still passed to Guzzle at runtime, but static analysis reports them; configure them on your own client instead.

Recipes ​

A crawler with a polite identity, hard limits and no redirects to other schemes:

php
$scraper = new Scraper([
    'connect_timeout' => 3,
    'timeout' => 10,
    'headers' => [
        'User-Agent' => 'AcmeBot/1.0 (+https://acme.example/bot)',
        'Accept-Language' => 'sk, en;q=0.8',
    ],
    'allow_redirects' => ['max' => 3, 'protocols' => ['https']],
]);

Parse error pages instead of throwing (soft 404 pages often still have a title):

php
$scraper = new Scraper(['http_errors' => false, 'timeout' => 10]);

Send everything through an egress proxy, except internal hosts:

php
$scraper = new Scraper([
    'proxy' => ['http' => 'http://proxy.internal:3128', 'https' => 'http://proxy.internal:3128', 'no' => ['.internal']],
    'timeout' => 10,
]);

Your own client with the scraper's defaults kept, plus a request option:

php
$client = new GuzzleHttp\Client([
    'connect_timeout' => 5,
    'headers' => ['User-Agent' => 'Tomaj\Scraper'],
    'cookies' => true,
]);
$scraper = new Scraper(['timeout' => 10], $client);

Released under the MIT License.