Preskočiť na obsah

Príklad: JSON-LD @graph ​

Spravodajská stránka tak, ako ju vykreslí WordPress s Yoast SEO: jeden blok JSON-LD s @graph so šiestimi uzlami (organizácia, web, obrázok, webová stránka, článok, osoba) prepojenými cez @id, druhý blok s navigáciou (breadcrumbs) a tagy Open Graph, ktorých titulok a obrázok sa líšia od tých v JSON-LD.

Stránka ​

html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rail strike: what changes for commuters on Monday - The Morning Post</title>
<meta name="description" content="Trains between Žilina and Košice stop for 24 hours; replacement buses run every hour.">
<meta property="og:type" content="article">
<meta property="og:title" content="Rail strike on Monday: what commuters need to know">
<meta property="og:site_name" content="The Morning Post">
<meta property="og:image" content="https://morningpost.example/img/strike-og.jpg">
<!-- WordPress + Yoast style: one block with a @graph, nodes linked by @id -->
<script type="application/ld+json" class="yoast-schema-graph">
{
  "@context": "https://schema.org",
  "@graph": [
    {
      "@type": "Organization",
      "@id": "https://morningpost.example/#organization",
      "name": "The Morning Post",
      "url": "https://morningpost.example/",
      "logo": {"@type": "ImageObject", "url": "https://morningpost.example/logo.png"}
    },
    {
      "@type": "WebSite",
      "@id": "https://morningpost.example/#website",
      "url": "https://morningpost.example/",
      "name": "The Morning Post",
      "description": "News from Slovakia every morning"
    },
    {
      "@type": "ImageObject",
      "@id": "https://morningpost.example/2026/11/rail-strike/#primaryimage",
      "url": "https://morningpost.example/img/strike-1600.jpg",
      "width": 1600,
      "height": 900
    },
    {
      "@type": "WebPage",
      "@id": "https://morningpost.example/2026/11/rail-strike/",
      "url": "https://morningpost.example/2026/11/rail-strike/",
      "name": "Rail strike: what changes for commuters on Monday - The Morning Post",
      "isPartOf": {"@id": "https://morningpost.example/#website"}
    },
    {
      "@type": ["NewsArticle", "Article"],
      "@id": "https://morningpost.example/2026/11/rail-strike/#article",
      "headline": "Rail strike: what changes for commuters on Monday",
      "description": "Trains between Žilina and Košice stop for 24 hours from 6 am on Monday.",
      "url": "https://morningpost.example/2026/11/rail-strike/",
      "image": {"@id": "https://morningpost.example/2026/11/rail-strike/#primaryimage"},
      "datePublished": "2026-11-22T17:05:00+01:00",
      "dateModified": "2026-11-23T07:40:00+01:00",
      "author": [
        {"@id": "https://morningpost.example/#/schema/person/peter"},
        {"@type": "Person", "name": "Eva Horváthová"}
      ],
      "articleSection": ["Slovakia", "Transport"],
      "about": [
        {"@type": "Thing", "@id": "https://morningpost.example/tag/strikes/", "name": "Strikes"},
        {"@type": "Thing", "@id": "https://morningpost.example/tag/railways/", "name": "Railways"}
      ],
      "publisher": {"@id": "https://morningpost.example/#organization"}
    },
    {
      "@type": "Person",
      "@id": "https://morningpost.example/#/schema/person/peter",
      "name": "Peter Novák",
      "url": "https://morningpost.example/author/peter/"
    }
  ]
}
</script>
<!-- a second block: ignored, BreadcrumbList does not describe the page -->
<script type="application/ld+json">
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [
  {"@type": "ListItem", "position": 1, "name": "Home", "item": "https://morningpost.example/"},
  {"@type": "ListItem", "position": 2, "name": "Slovakia", "item": "https://morningpost.example/slovakia/"}
]}
</script>
</head>
<body><h1>Rail strike: what changes for commuters on Monday</h1></body>
</html>

Kód ​

php
<?php

declare(strict_types=1);

// Reads the schema.org JSON-LD of a WordPress/Yoast style article (a @graph with @id references)
// and shows how the order of the parsers decides which value wins.

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

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

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

$scraper = new Scraper(['timeout' => 5]);

// 1. JSON-LD only. SchemaParser picks the NewsArticle node (not the Organization, WebSite,
//    ImageObject or WebPage nodes) and resolves the {"@id": ...} references of author and image.
$meta = $scraper->parseUrl($base . '/news.html', [new SchemaParser()]);

echo "== SchemaParser\n";
printf("%-12s %s\n", 'title', $meta->getTitle());
printf("%-12s %s\n", 'description', $meta->getDescription());
printf("%-12s %s\n", 'url', $meta->getOgUrl());
printf("%-12s %s\n", 'image', $meta->getOgImage());
printf("%-12s %s\n", 'published', $meta->getPublishedTime()?->format(DATE_ATOM));
printf("%-12s %s\n", 'modified', $meta->getModifiedTime()?->format(DATE_ATOM));
foreach ($meta->getAuthors() as $author) {
    printf("%-12s %s (%s)\n", 'author', $author->name(), $author->id() ?? 'no @id');
}
foreach ($meta->getSections() as $section) {
    printf("%-12s %s\n", 'section', $section->name());
}
foreach ($meta->getTags() as $tag) {
    printf("%-12s %s (%s)\n", 'tag', $tag->name(), $tag->id());
}
// Open Graph fields that JSON-LD has no equivalent for stay empty
printf("%-12s %s\n", 'og:title', $meta->getOgTitle() ?? '(null)');
printf("%-12s %s\n", 'site name', $meta->getOgSiteName() ?? '(null)');

// 2. Both parsers. The first one that finds a value wins, the second fills the gaps.
$html = $scraper->getBody();
$fields = static fn(Meta $meta): array => [
    'title' => $meta->getTitle(),
    'description' => $meta->getDescription(),
    'og:title' => $meta->getOgTitle(),
    'og:image' => $meta->getOgImage(),
    'site name' => $meta->getOgSiteName(),
    'authors' => implode(', ', array_map(static fn($author): ?string => $author->name(), $meta->getAuthors())),
];

foreach (['SchemaParser, OgDomParser' => [new SchemaParser(), new OgDomParser()], 'OgDomParser, SchemaParser' => [new OgDomParser(), new SchemaParser()]] as $order => $parsers) {
    echo "\n== {$order}\n";
    foreach ($fields($scraper->parse($html, $parsers)) as $name => $value) {
        printf("%-12s %s\n", $name, $value);
    }
}

Výstup ​

text
== SchemaParser
title        Rail strike: what changes for commuters on Monday
description  Trains between Žilina and Košice stop for 24 hours from 6 am on Monday.
url          https://morningpost.example/2026/11/rail-strike/
image        https://morningpost.example/img/strike-1600.jpg
published    2026-11-22T17:05:00+01:00
modified     2026-11-23T07:40:00+01:00
author       Peter Novák (https://morningpost.example/#/schema/person/peter)
author       Eva Horváthová (no @id)
section      Slovakia
section      Transport
tag          Strikes (https://morningpost.example/tag/strikes/)
tag          Railways (https://morningpost.example/tag/railways/)
og:title     (null)
site name    (null)

== SchemaParser, OgDomParser
title        Rail strike: what changes for commuters on Monday
description  Trains between Žilina and Košice stop for 24 hours from 6 am on Monday.
og:title     Rail strike on Monday: what commuters need to know
og:image     https://morningpost.example/img/strike-1600.jpg
site name    The Morning Post
authors      Peter Novák, Eva Horváthová

== OgDomParser, SchemaParser
title        Rail strike: what changes for commuters on Monday - The Morning Post
description  Trains between Žilina and Košice stop for 24 hours; replacement buses run every hour.
og:title     Rail strike on Monday: what commuters need to know
og:image     https://morningpost.example/img/strike-og.jpg
site name    The Morning Post
authors      Peter Novák, Eva Horváthová

Čo si všimnúť ​

  • Správny uzol. SchemaParser použije NewsArticle (jeho @type je zoznam, ["NewsArticle", "Article"]) a ignoruje uzly Organization, WebSite, ImageObject, WebPage a BreadcrumbList, hoci sú skôr a majú vlastné url, name a description. Pozrite Ktorý uzol sa použije.
  • Vyriešené odkazy. Prvý autor je v článku len {"@id": ".../person/peter"}; parser nájde uzol Person a vráti jeho meno spolu s @id. Druhý autor je zapísaný priamo a @id nemá. Obrázok je odkaz na uzol ImageObject.
  • Žiadny Open Graph z JSON-LD. og:title a názov webu ostanú pri samotnom SchemaParser null.
  • Rozhoduje poradie. Keď je prvý SchemaParser, titulok je čistý headline a obrázok je ten so šírkou 1600 px z JSON-LD. Keď je prvý OgDomParser, dostanete <title> s príponou webu, meta description a obrázok pre sociálne siete. Autori sú v oboch prípadoch rovnakí: stránka nemá meta tag author, takže ich má len JSON-LD. Pozrite Kombinovanie parserov.

Čo kontroluje CI ​

Vybraný uzol, oba vyriešené odkazy, dátumy s posunom, sekcie a tagy s ich @id a zlúčený výsledok pri oboch poradiach parserov.