Skip to content

Meta ScraperPage metadata for PHP, in one object

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.

An HTML page with title, Open Graph and JSON-LD tags goes through SchemaParser, OgDomParser and your own parser and becomes one Meta object with the title, an image, the authors and the publication dateAn HTML page with title, Open Graph and JSON-LD tags goes through SchemaParser, OgDomParser and your own parser and becomes one Meta object with the title, an image, the authors and the publication date

What it looks like ​

parseUrl downloads the page with Guzzle, parse takes HTML you already have; the parsers run in order, each returns a new Meta, merge keeps the first value found, and the result is one Meta objectparseUrl downloads the page with Guzzle, parse takes HTML you already have; the parsers run in order, each returns a new Meta, merge keeps the first value found, and the result is one Meta object

Give the scraper HTML (or a URL) and the parsers you want, in the order you trust them:

php
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";
text
Ten tips for PHP 8.4
https://blog.example/php84.jpg
Jana Nováková
2024-11-21 08:00 +00:00

The 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.

Where to go next ​

FAQ ​

What does Meta Scraper do? ​

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.

Which PHP version does it need? ​

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.

Does it run JavaScript? ​

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.

Is it safe to scrape URLs that users paste? ​

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.

Released under the MIT License.