Combining parsers
Scraper::parse($html, $parsers) runs the parsers in the order of the array and merges their results into one Meta with Meta::merge().
The rules
- The first parser that finds a value wins. A later parser fills only fields that are still empty.
- A text field is empty when it is
null,''or'0'. A parser that found''(for example<meta name="description" content="">) does not block a later parser. - Lists are taken whole. Authors, sections and tags come from the first parser whose list is not empty, and are never mixed: JSON-LD authors with
@idare not extended with a duplicate name from<meta name="author">. - The published and modified times are taken from the first parser that parsed a date.
- Each field is decided on its own: the title may come from one parser and the image from another.
The order changes the result
php
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use Tomaj\Scraper\Parser\OgDomParser;
use Tomaj\Scraper\Parser\SchemaParser;
use Tomaj\Scraper\Scraper;
$html = '<title>Page title | Site</title>'
. '<meta property="og:title" content="Open Graph title">'
. '<meta property="og:site_name" content="Site">'
. '<script type="application/ld+json">{"@type":"NewsArticle","headline":"Schema headline"}</script>';
$scraper = new Scraper();
// SchemaParser first: its headline wins, OgDomParser fills what is still missing
$meta = $scraper->parse($html, [new SchemaParser(), new OgDomParser()]);
echo $meta->getTitle(), ' / ', $meta->getOgSiteName(), "\n";
// OgDomParser first: the <title> wins
$meta = $scraper->parse($html, [new OgDomParser(), new SchemaParser()]);
echo $meta->getTitle(), ' / ', $meta->getOgSiteName(), "\n";Output:
text
Schema headline / Site
Page title | Site / SiteTypical orders:
| Order | Gives |
|---|---|
[new SchemaParser(), new OgDomParser()] | The editorial headline (no " | Site" suffix), authors with @id, exact dates; Open Graph fills the image, site name and the og:* fields. Good for archiving and indexing articles. |
[new OgDomParser(), new SchemaParser()] | What the page shows when shared: the <title>, the og:image chosen for social networks. JSON-LD only fills the gaps (authors with @id when there is no author meta tag). Good for link previews. |
[…, new YourParser()] | Your parser as the last fallback, for example for Twitter cards. |
The JSON-LD @graph example runs both orders on one page and prints which value won.
getTitle() versus getOgTitle()
They are different fields. getTitle() is the <title> (or the JSON-LD headline), getOgTitle() is og:title. A link preview usually wants og:title first:
php
$meta = $scraper->parse($html, [new OgDomParser()]);
$headline = $meta->getOgTitle() ?? $meta->getTitle();
echo $headline, "\n";text
Ten tips for PHP 8.4Merging yourself
Meta::merge() is public, so you can combine results that you produced separately, for example cached JSON-LD with freshly downloaded Open Graph. It changes and returns the object it is called on:
php
$fromCache = new Meta()->setTitle('Cached title');
$fresh = new OgDomParser()->parse($html);
$meta = $fromCache->merge($fresh);
echo $meta->getTitle(), "\n"; // kept: it was already set
echo $meta->getOgImage(), "\n"; // filled from $freshtext
Cached title
https://blog.example/php84.jpg