Skip to content

Upgrading from meta-scraper 4.x to 5.0 ​

This file lists the changes in 5.0 that may need work in your application. The full list of changes is in CHANGELOG.md.

Checklist:

  • PHP 8.4 and Guzzle 7.15.2+ or 8: Requirements.
  • parseUrl() throws FetchException instead of Guzzle exceptions: Exceptions.
  • Classes are final, value objects are readonly, protected members are gone: Final classes.
  • Scraper options are typed; options outside the documented list go into your own Guzzle client: Scraper options.
  • OgDomParser throws without ext-dom: OgDomParser.
  • Published and modified times are DateTimeImmutable with the page's offset; relative dates are ignored: Times.
  • Parsers return different (correct) values for some pages: Parser output.

Requirements ​

  • PHP 8.4 or newer (4.x ran on PHP 7.1+).
  • guzzlehttp/guzzle ^7.15.2 || ^8.0.1. Guzzle 6 is no longer supported; older Guzzle 7 releases have published security advisories.

parseUrl() throws FetchException ​

Before, a failed download let the Guzzle exception escape. Now every failure (connection error, timeout, TLS error, too many redirects, 4xx/5xx response) is a Tomaj\Scraper\Exception\FetchException. The Guzzle exception is its previous exception. All exceptions of the package implement Tomaj\Scraper\Exception\ScraperException.

php
// 4.x
try {
    $meta = $scraper->parseUrl($url, $parsers);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // ...
}

// 5.0
try {
    $meta = $scraper->parseUrl($url, $parsers);
} catch (\Tomaj\Scraper\Exception\FetchException $e) {
    $guzzleException = $e->getPrevious();
    $failedUrl = $e->url;
}

Classes are final ​

Scraper, Meta, Author, Section, Tag, OgParser, OgDomParser and SchemaParser are final; Author, Section and Tag are readonly. Scraper::$guzzleOptions, OgDomParser::$meta, OgDomParser::$allowedAttributes, OgDomParser::processMetaTag() and OgDomParser::fixMetaTag() (all protected) are removed.

If you extended a class, compose it instead, or implement ParserInterface:

php
// 4.x
class MyParser extends OgParser
{
    public function parse(string $content): Meta
    {
        $meta = parent::parse($content);
        // ...
        return $meta;
    }
}

// 5.0
final class MyParser implements ParserInterface
{
    public function __construct(private readonly OgParser $og = new OgParser()) {}

    public function parse(string $content): Meta
    {
        $meta = $this->og->parse($content);
        // ...
        return $meta;
    }
}

If you changed the default Guzzle options by overriding Scraper::$guzzleOptions, pass them to the constructor instead (next section).

Scraper options and your own Guzzle client ​

new Scraper(array $guzzleOptions = []) still works and the options are still merged over the defaults (5 second connect timeout, Tomaj\Scraper User-Agent). The options array is now typed for static analysis: connect_timeout, timeout, read_timeout, headers, proxy, allow_redirects, verify, http_errors, decode_content, force_ip_resolve and version. Other Guzzle options still reach Guzzle at runtime, but PHPStan and Psalm report them. For those, pass your own client as the second argument; the defaults are not applied to it, and the options array becomes request options:

php
// 4.x
$scraper = new Scraper(['handler' => $stack, 'cookies' => true]);

// 5.0
$scraper = new Scraper([], new \GuzzleHttp\Client([
    'handler' => $stack,
    'cookies' => true,
    'connect_timeout' => 5,
    'headers' => ['User-Agent' => 'MyBot/1.0'],
]));

The client is created once in the constructor instead of on every parseUrl() call.

OgDomParser requires ext-dom and ext-libxml ​

Without ext-dom or ext-libxml, new OgDomParser() used to succeed and then fail inside parse() (or find nothing). It now throws Tomaj\Scraper\Exception\MissingExtensionException (a LogicException). Use OgParser where the extensions are not available.

Published and modified times ​

Meta::getPublishedTime() and getModifiedTime() return ?DateTimeImmutable instead of ?DateTime (#36). The setters accept any DateTimeInterface (stored as a copy) or a string.

  • Immutable. modify(), setTimezone() and friends return a new object and no longer change the Meta. Code that called them for their side effect must use the return value. Code that typed a variable or parameter as DateTime must accept DateTimeImmutable (or DateTimeInterface).
  • The page's offset is kept. 4.x converted every string to UTC: 2018-12-15T19:00:26+02:00 came back as 2018-12-15T17:00:26+00:00. Now it comes back with +02:00; the instant is the same, so comparisons and timestamps do not change, but format() shows the page's local time. Call setTimezone() for the zone you display.
  • Only absolute dates. 4.x used strtotime(), which also accepted relative expressions (yesterday noon, +1 week, now) and resolved them against the server's clock. Those, a time without a date, a Unix timestamp (@1763616600), an invalid date (2026-02-30) and an RFC 2822 date whose weekday does not match are now ignored (the field stays as it was). A string without an offset is still in the default time zone.
php
// 4.x
$published = $meta->getPublishedTime();          // DateTime in UTC
$published->setTimezone(new DateTimeZone('Europe/Bratislava')); // also changed $meta
echo $published->format('j. n. Y H:i');

// 5.0
$published = $meta->getPublishedTime();          // DateTimeImmutable with the page's offset
echo $published?->setTimezone(new DateTimeZone('Europe/Bratislava'))->format('j. n. Y H:i');

Parser output changes ​

These are bug fixes, but they change what the parsers return:

  • OgDomParser returns a new Meta from every parse() call. Before, one parser instance returned the same object every time, with authors and sections piling up across pages (#21). If you relied on that accumulation, merge the results yourself with Meta::merge().
  • OgDomParser reads documents without a charset declaration as UTF-8. Before, their non-ASCII text came back garbled (Žltý instead of Žltý) (#22).
  • SchemaParser reads standard JSON-LD (<script type="application/ld+json"> without id="schema", multiline JSON, several blocks, @graph, {"@id": …} references). Pages that used to give an empty result from SchemaParser may now return a title, authors, dates and more. When SchemaParser runs first, its values win over the Open Graph ones, so getTitle() may now be the article headline instead of the <title> (#23). Pages with an id="schema" block keep using it.
  • SchemaParser also reads image as a URL string, a list or an ImageObject with only a contentUrl (#30). When it runs before an Open Graph parser, getOgImage() may now be the JSON-LD image instead of og:image.
  • OgDomParser no longer changes the libxml_use_internal_errors() setting; 4.x left internal errors switched on for the rest of the process.
  • OgDomParser parses with the HTML5 parser of PHP 8.4 (Dom\HTMLDocument) instead of libxml's HTML 4 parser (#34). Entities are decoded once: a double-escaped Tom &amp;amp; Jerry is now Tom &amp; Jerry (4.x returned Tom & Jerry), and HTML5-only entities such as &ccaron; are decoded. A charset declaration is read from the first 1024 bytes like in a browser; later declarations and XML declarations are still used for pages that are not valid UTF-8. Values are always valid UTF-8. A <title> inside an inline <svg> is no longer taken as the page title.
  • OgParser returns null for a tag it cannot read (content before name/property, no content) and an empty string for content="". 4.x returned the content of a later tag or a piece of markup instead. Values with the other quote character (content="Don't") are no longer cut at it (#29).
  • OgParser decodes every HTML entity (&eacute;, &hellip;, &nbsp;, &#8211; …), not only &amp;, &lt;, &gt;, &quot; and &#039;. Code that decoded the values a second time may now decode text that the page meant literally; &nbsp; becomes U+00A0, not a plain space (#35).

See also ​

Released under the MIT License.