Skip to content

Example: Open Graph article ​

A news article with the usual tags: <title>, description, keywords, author, Open Graph, two article:section tags and the published and modified times. run.php downloads it with parseUrl() and prints every field, first with OgDomParser, then with OgParser on the same HTML from getBody().

The page ​

html
<!DOCTYPE html>
<html lang="sk">
<head>
    <meta charset="utf-8">
    <title>Bratislava opens its first night tram line | City Daily</title>
    <meta name="description" content="Line N1 runs every 20 minutes from Račianske mýto to Petržalka, from midnight to 4 am.">
    <meta name="keywords" content="Bratislava, public transport, trams, night lines">
    <meta name="author" content="Zuzana Kováčová">
    <meta property="og:type" content="article">
    <meta property="og:title" content="Bratislava opens its first night tram line">
    <meta property="og:description" content="Line N1 runs every 20 minutes, from midnight to 4 am.">
    <meta property="og:url" content="https://citydaily.example/2026/11/night-tram">
    <meta property="og:site_name" content="City Daily">
    <meta property="og:image" content="https://citydaily.example/img/night-tram-1200.jpg">
    <meta property="article:section" content="City">
    <meta property="article:section" content="Transport">
    <meta property="article:published_time" content="2026-11-20T06:30:00+01:00">
    <meta property="article:modified_time" content="2026-11-20T09:15:00+01:00">
    <meta name="twitter:card" content="summary_large_image">
</head>
<body>
    <article>
        <h1>Bratislava opens its first night tram line</h1>
        <p>Line N1 runs every 20 minutes &hellip;</p>
    </article>
</body>
</html>

The code ​

php
<?php

declare(strict_types=1);

// Downloads a news article and prints every field the meta tag parsers read from it.
// The page is a fixture (site/article.html) served by `php -S`, see e2e.sh.

require __DIR__ . '/vendor/autoload.php';

use Tomaj\Scraper\Meta;
use Tomaj\Scraper\Parser\OgDomParser;
use Tomaj\Scraper\Parser\OgParser;
use Tomaj\Scraper\Scraper;

$base = $argv[1] ?? 'http://127.0.0.1:8401';

$scraper = new Scraper(['timeout' => 5]);
$meta = $scraper->parseUrl($base . '/article.html', [new OgDomParser()]);

function show(Meta $meta): void
{
    $names = static fn(array $items): string => implode(', ', array_map(static fn($item): ?string => $item->name(), $items));

    printf("%-15s %s\n", 'title', $meta->getTitle());
    printf("%-15s %s\n", 'description', $meta->getDescription());
    printf("%-15s %s\n", 'keywords', $meta->getKeywords());
    printf("%-15s %s\n", 'authors', $names($meta->getAuthors()));
    printf("%-15s %s\n", 'og:type', $meta->getOgType());
    printf("%-15s %s\n", 'og:title', $meta->getOgTitle());
    printf("%-15s %s\n", 'og:description', $meta->getOgDescription());
    printf("%-15s %s\n", 'og:url', $meta->getOgUrl());
    printf("%-15s %s\n", 'og:site_name', $meta->getOgSiteName());
    printf("%-15s %s\n", 'og:image', $meta->getOgImage());
    printf("%-15s %s\n", 'sections', $names($meta->getSections()));
    // times are DateTimeImmutable objects with the offset the page wrote
    printf("%-15s %s\n", 'published', $meta->getPublishedTime()?->format(DATE_ATOM));
    printf("%-15s %s\n", 'modified', $meta->getModifiedTime()?->format(DATE_ATOM));
}

echo "== OgDomParser\n";
show($meta);

// The regular expression parser reads the same tags without ext-dom, but only the first
// occurrence of each one: the second article:section is missing.
echo "\n== OgParser (the same HTML, from getBody())\n";
show($scraper->parse($scraper->getBody(), [new OgParser()]));

Output ​

text
== OgDomParser
title           Bratislava opens its first night tram line | City Daily
description     Line N1 runs every 20 minutes from Račianske mýto to Petržalka, from midnight to 4 am.
keywords        Bratislava, public transport, trams, night lines
authors         Zuzana Kováčová
og:type         article
og:title        Bratislava opens its first night tram line
og:description  Line N1 runs every 20 minutes, from midnight to 4 am.
og:url          https://citydaily.example/2026/11/night-tram
og:site_name    City Daily
og:image        https://citydaily.example/img/night-tram-1200.jpg
sections        City, Transport
published       2026-11-20T06:30:00+01:00
modified        2026-11-20T09:15:00+01:00

== OgParser (the same HTML, from getBody())
title           Bratislava opens its first night tram line | City Daily
description     Line N1 runs every 20 minutes from Račianske mýto to Petržalka, from midnight to 4 am.
keywords        Bratislava, public transport, trams, night lines
authors         Zuzana Kováčová
og:type         article
og:title        Bratislava opens its first night tram line
og:description  Line N1 runs every 20 minutes, from midnight to 4 am.
og:url          https://citydaily.example/2026/11/night-tram
og:site_name    City Daily
og:image        https://citydaily.example/img/night-tram-1200.jpg
sections        City
published       2026-11-20T06:30:00+01:00
modified        2026-11-20T09:15:00+01:00

What to notice ​

  • Times keep the page's offset. 2026-11-20T06:30:00+01:00 comes back as a DateTimeImmutable with +01:00. See Dates.
  • Sections: the page has two article:section tags. OgDomParser returns both, OgParser only the first. Everything else is the same for this well-formed page.
  • Non-ASCII text (Račianske mýto, Kováčová) comes through unchanged: the page is UTF-8 and declares it.
  • twitter:card is ignored: no built-in parser reads Twitter tags. The Twitter card example adds a parser for them.
  • getBody() returns the HTML of the last download, so the second parse needs no second request.

What CI checks ​

The output must match expected-output.txt exactly: every field read by OgDomParser and OgParser, both dates with their offset and the difference in sections.

Released under the MIT License.