tomaj / meta-scraper
Extracts title, description, Open Graph and schema.org JSON-LD metadata from HTML pages.
Requires
- php: >= 7.1.0
- guzzlehttp/guzzle: ^6.0 | ^7.0
Requires (Dev)
- codeclimate/php-test-reporter: 0.4.4
- phpunit/phpunit: ^8 || ^9
- squizlabs/php_codesniffer: ^3.5
Suggests
- ext-dom: Required for Tomaj\Scraper\Parser\OgDomParser
- ext-libxml: Required for Tomaj\Scraper\Parser\OgDomParser
Provides
None
Conflicts
None
Replaces
None
README
Meta Scraper
Extracts title, description, Open Graph and schema.org JSON-LD metadata from HTML pages.
Documentation: https://tomaj.github.io/meta-scraper/
Getting started · Parsers · Examples · API reference · Changelog
What is meta-scraper?
meta-scraper reads the metadata a web page publishes about itself and returns it as one Meta object:
- the
<title>and thedescription,keywordsandauthormeta tags, - Open Graph properties (
og:title,og:description,og:type,og:url,og:site_name,og:image,article:section,article:published_time,article:modified_time), - schema.org JSON-LD (
<script type="application/ld+json">): headline, description, authors, sections, tags, image, URL and dates.
Each source has its own parser. Combine them and the first parser that finds a value wins.
Installation
Requires PHP 8.4 or newer.
composer require tomaj/meta-scraper
Usage
use Tomaj\Scraper\Parser\OgParser;
use Tomaj\Scraper\Parser\SchemaParser;
use Tomaj\Scraper\Scraper;
$scraper = new Scraper();
// parse HTML you already have
$meta = $scraper->parse($html, [new SchemaParser(), new OgParser()]);
// or download the page with Guzzle and parse it
$meta = $scraper->parseUrl($url, [new SchemaParser(), new OgParser()]);
echo $meta->getTitle();
echo $meta->getOgImage();
foreach ($meta->getAuthors() as $author) {
echo $author->name();
}
Parsers, fetching options (timeouts, User-Agent, your own Guzzle client), error handling and the full API are described in the documentation.
Contributing
Contributions are welcome. See CONTRIBUTING.md and the Code of Conduct.
Security
Do not report security vulnerabilities in public issues. See SECURITY.md; it also explains why parseUrl() must not receive untrusted URLs without SSRF protection.
License
MIT. See LICENSE.md.