askancy / metacritic-api-php
PHP API scraper for Metacritic game scores, returns JSON data.
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0
- symfony/css-selector: ^6.0
- symfony/dom-crawler: ^6.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
Level up your apps with real-time Metacritic scores using this lightweight PHP package.
No API keys, no rate limits โ just smart scraping and structured JSON output. ๐ง โก
๐ฆ Install & Deploy
Install it via Composer and get your first score in less than 60 seconds:
composer require askancy/metacritic-api-php
๐น๏ธ How to Play (Usage)
require 'vendor/autoload.php'; use Askancy\Metacritic\MetacriticScraper; use Askancy\Metacritic\Exceptions\MetacriticException; $scraper = new MetacriticScraper(); try { $result = $scraper->fetch('elden-ring'); // Use the Metacritic slug echo json_encode($result, JSON_PRETTY_PRINT); } catch (MetacriticException $e) { echo "๐ฅ Error: " . $e->getMessage(); }
๐ง Pro Tip: You can pass any valid slug like 'god-of-war-ragnarok'
, 'cyberpunk-2077'
, 'final-fantasy-vii-remake'
, etc.
๐งพ Sample JSON Response
{ "slug": "elden-ring", "url": "https://www.metacritic.com/game/elden-ring", "score": "96" }
slug
: Slug used to build the URLurl
: Full Metacritic link to the game pagescore
: Metacritic metascore (0-100)
๐งช Unit Testing (PHPUnit)
Run the full test suite to validate your setup:
composer install vendor/bin/phpunit tests/
โ Includes:
- Score presence and format check
- Proper exception handling on 404 pages
- JSON serialization test
๐ ๏ธ Directory Structure
metacritic-api-php/
โโโ src/
โ โโโ MetacriticScraper.php # Main class
โ โโโ Exceptions/
โ โโโ MetacriticException.php # Custom error
โโโ tests/
โ โโโ MetacriticScraperTest.php # PHPUnit test
โโโ composer.json
โโโ README.md
๐งโโ๏ธ Advanced Features
- โ No API key required
- โ ๏ธ Graceful error handling (timeouts, 404s)
- ๐งฉ Easily extendable to retrieve more metadata (e.g., platforms, release date)
- ๐ต๏ธโโ๏ธ Smart fallback: if JSON-LD fails, uses HTML metascore node
๐ง Game Dev Friendly
This package is perfect for:
- Game databases ๐๏ธ
- Gaming communities ๐งโ๐คโ๐ง
- Game recommendation engines ๐ค
- Gamified profiles or dashboards ๐
๐ License
This package is licensed under the MIT License, with one twist:
โ Redistribution and reselling is NOT allowed. ๐ Contributions and improvements are always welcome.
๐ Credits & Contact
Made with โค๏ธ by Askancy For support, bugs or PRs: GitHub Issues
๐ฌ "Always check the score, before you hit Play." ๐ฎ