Meta tags and Open Graph
<title>, description, keywords and author, og:title, og:description, og:type, og:url, og:site_name, og:image, article:section and the published and modified times.
OgParser and OgDomParser
Title, description, authors, image, dates, sections and tags from the meta tags, Open Graph and schema.org JSON-LD of any HTML page. Combine parsers, download with Guzzle, get one Meta object back.
Give the scraper HTML (or a URL) and the parsers you want, in the order you trust them:
use Tomaj\Scraper\Parser\OgDomParser;
use Tomaj\Scraper\Parser\SchemaParser;
use Tomaj\Scraper\Scraper;
$meta = new Scraper()->parse($html, [new SchemaParser(), new OgDomParser()]);
echo $meta->getTitle(), "\n";
echo $meta->getOgImage(), "\n";
echo $meta->getAuthors()[0]->name(), "\n";
echo $meta->getPublishedTime()?->format('Y-m-d H:i e'), "\n";Ten tips for PHP 8.4
https://blog.example/php84.jpg
Jana Nováková
2024-11-21 08:00 +00:00The title and the author come from the JSON-LD BlogPosting, the image from og:image, which JSON-LD did not have. examples/open-graph-article runs the same against a real web server.
It reads the metadata a web page publishes about itself: the <title> and meta tags, Open Graph properties and schema.org JSON-LD, and returns it as one Meta object with a getter per field. See Getting started.
PHP 8.4 or newer and Guzzle 7.15.2+ or 8. OgDomParser also needs ext-dom and ext-libxml, which most PHP builds have. See Requirements.
No. It parses the HTML the server returns. For pages that build their metadata in the browser, render them with a headless browser and pass the HTML to parse(). See the FAQ.
Only with protection against server-side request forgery: allow http(s) only, reject private addresses, check redirects and set timeouts. The link preview example shows a complete guard. See Security.