Skip to content

Examples ​

Every example is a small Composer project in examples/ of the repository. It scrapes fixture pages served by PHP's built-in web server (php -S) on 127.0.0.1, so it needs no internet and gives the same output every time. The pages on this site include the code straight from those files.

ExampleWhat it shows
Open Graph articleEvery meta tag and Open Graph field of a news article, OgDomParser and OgParser side by side
JSON-LD @graphA Yoast-style @graph with @id references; how the parser order decides which value wins
Twitter card parserA custom ParserInterface that reads twitter:* tags as a fallback
Broken pagesReversed attributes, unquoted tags, invalid JSON-LD, UTF-8 without a declaration, Windows-1250, an empty page
Fetch errors404, 500, timeout, redirect loop, refused connection, http_errors and allow_redirects
Link preview (SSRF-safe)Preview cards for user-submitted URLs: URL guard, pinned DNS, checked redirects, escaped output
Testing with a mockUnit-testing code that calls parseUrl() with a Guzzle MockHandler

How they are tested ​

Each example has:

  • composer.json requiring tomaj/meta-scraper,
  • run.php, the example itself, which prints what it found,
  • site/ with the fixture pages (and a router.php where a page needs a special status, header or delay),
  • e2e.sh, which starts php -S for site/, runs run.php and compares its output with
  • expected-output.txt.

The Examples workflow runs every e2e.sh on every push and pull request. examples/ci/use-local-meta-scraper.sh installs the package from the checkout through a Composer path repository, so the examples always run against the code of the pull request, not a released version. When a change to the library changes an example's output, the workflow fails and shows the difference.

Run one yourself ​

bash
git clone https://github.com/tomaj/meta-scraper.git
cd meta-scraper
examples/ci/recipe.sh link-preview

recipe.sh copies the example to a temporary directory, installs it against your checkout and runs its e2e.sh. It needs PHP 8.4+, Composer and free ports 8401 to 8406.

Released under the MIT License.