Preskočiť na obsah

Príklad: článok s Open Graph ​

Spravodajský článok s bežnými tagmi: <title>, description, keywords, author, Open Graph, dva tagy article:section a čas publikovania a úpravy. run.php ho stiahne cez parseUrl() a vypíše každé pole, najprv s OgDomParser, potom s OgParser nad tým istým HTML z getBody().

Stránka ​

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>

Kód ​

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()]));

Výstup ​

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

Čo si všimnúť ​

  • Časy si zachovajú posun zo stránky. 2026-11-20T06:30:00+01:00 sa vráti ako DateTimeImmutable s +01:00. Pozrite Dátumy.
  • Sekcie: stránka má dva tagy article:section. OgDomParser vráti oba, OgParser len prvý. Všetko ostatné je pri tejto správne zapísanej stránke rovnaké.
  • Text s diakritikou (Račianske mýto, Kováčová) prejde nezmenený: stránka je v UTF-8 a deklaruje to.
  • twitter:card sa ignoruje: tagy Twitteru nečíta žiadny vstavaný parser. Príklad s Twitter kartami pre ne pridáva parser.
  • getBody() vracia HTML z posledného sťahovania, takže druhé parsovanie nepotrebuje druhú požiadavku.

Čo kontroluje CI ​

Výstup sa musí presne zhodovať s expected-output.txt: každé pole prečítané parsermi OgDomParser a OgParser, oba dátumy s ich posunom a rozdiel v sekciách.