Skip to content

Getting started ​

Requirements ​

RequirementVersionWhy
PHP8.4 or newer
guzzlehttp/guzzle^7.15.2 or ^8.0.1installed with the package, used by parseUrl()
ext-dom, ext-libxmlanyonly for OgDomParser; enabled in most PHP builds

Installation ​

bash
composer require tomaj/meta-scraper

Your first page ​

Scraper::parse() takes the HTML and a list of parsers. Each parser reads one kind of metadata; the scraper merges their results into one Meta object:

php
<?php

declare(strict_types=1);

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

use Tomaj\Scraper\Parser\OgParser;
use Tomaj\Scraper\Parser\SchemaParser;
use Tomaj\Scraper\Scraper;

$html = <<<'HTML'
    <html>
    <head>
        <title>Ten tips for PHP 8.4 | Example Blog</title>
        <meta name="description" content="What changed in PHP 8.4 and how to use it.">
        <meta property="og:title" content="Ten tips for PHP 8.4">
        <meta property="og:image" content="https://blog.example/php84.jpg">
        <script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "BlogPosting",
            "headline": "Ten tips for PHP 8.4",
            "author": {"@type": "Person", "name": "Jana Nováková"},
            "datePublished": "2024-11-21T08:00:00+00:00"
        }
        </script>
    </head>
    </html>
    HTML;

$meta = new Scraper()->parse($html, [new SchemaParser(), new OgParser()]);

echo $meta->getTitle(), "\n";
echo $meta->getDescription(), "\n";
echo $meta->getOgImage(), "\n";
foreach ($meta->getAuthors() as $author) {
    echo $author->name(), "\n";
}
echo $meta->getPublishedTime()?->format('Y-m-d'), "\n";

Output:

text
Ten tips for PHP 8.4
What changed in PHP 8.4 and how to use it.
https://blog.example/php84.jpg
Jana Nováková
2024-11-21

The title is the JSON-LD headline, because SchemaParser runs first. The description and the image come from the meta tags, which SchemaParser did not find. Combining parsers explains the rules.

Downloading a page ​

parseUrl() downloads the page with a GET request and parses it. getBody() returns the HTML it downloaded:

php
use Tomaj\Scraper\Exception\FetchException;

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

try {
    $meta = $scraper->parseUrl($url, [new SchemaParser(), new OgDomParser()]);
    echo $meta->getOgSiteName(), ': ', $meta->getTitle(), "\n";
    echo strlen($scraper->getBody()) > 0 ? "body downloaded\n" : "empty body\n";
} catch (FetchException $e) {
    echo "Cannot download {$e->url}\n";
}
text
Example Blog: Ten tips for PHP 8.4
body downloaded

Timeouts, headers, proxies and your own Guzzle client are in Fetching pages; every option is in the configuration reference.

URLs from users

Do not pass a URL that a user typed to parseUrl() without protection against server-side request forgery. See Security.

What you get ​

Meta has a getter for every field. A field that no parser found is null, a list that no parser found is []:

GetterTypeRead from
getTitle()?string<title>, JSON-LD headline
getDescription()?string<meta name="description">, JSON-LD description
getKeywords()?string<meta name="keywords">
getAuthors()list<Author><meta name="author">, JSON-LD author
getSections()list<Section>article:section, JSON-LD articleTerms / articleSection
getTags()list<Tag>JSON-LD about
getOgType()?stringog:type
getOgTitle()?stringog:title
getOgDescription()?stringog:description
getOgUrl()?stringog:url, JSON-LD url
getOgSiteName()?stringog:site_name
getOgImage()?stringog:image, JSON-LD image.url
getPublishedTime()?DateTimeImmutable (the page's offset)article:published_time, JSON-LD datePublished
getModifiedTime()?DateTimeImmutable (the page's offset)article:modified_time, JSON-LD dateModified

The Meta object describes every field; the API reference lists every class and method.

Next steps ​

  • How it works: what happens between the HTML and the Meta object.
  • Choosing a parser: which parsers to use, and in which order.
  • Examples: seven runnable recipes, from a news article to an SSRF-safe link preview.

Released under the MIT License.