Getting started
Requirements
| Requirement | Version | Why |
|---|---|---|
| PHP | 8.4 or newer | |
guzzlehttp/guzzle | ^7.15.2 or ^8.0.1 | installed with the package, used by parseUrl() |
ext-dom, ext-libxml | any | only for OgDomParser; enabled in most PHP builds |
Installation
composer require tomaj/meta-scraperYour 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
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:
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-21The 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:
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";
}Example Blog: Ten tips for PHP 8.4
body downloadedTimeouts, 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 []:
| Getter | Type | Read 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() | ?string | og:type |
getOgTitle() | ?string | og:title |
getOgDescription() | ?string | og:description |
getOgUrl() | ?string | og:url, JSON-LD url |
getOgSiteName() | ?string | og:site_name |
getOgImage() | ?string | og: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
Metaobject. - 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.