zero-to-prod / omdb
A PHP wrapper for the OMDb API with full object support
Maintainers
Details
Fund package maintenance!
Github
Requires
- php: >=8.1.0
- ext-curl: *
- zero-to-prod/omdb-api: ^1.0.1
- zero-to-prod/omdb-models: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10.0
Suggests
- zero-to-prod/data-model: Transform Data to Type-Safe DTOs
- zero-to-prod/data-model-helper: Helpers for a DataModel.
README
Contents
Introduction
Zerotoprod\OmdbApi
is a PHP cURL wrapper for the OMDb API with full object support.
It allows you to search for movies, series, and other media, retrieve detailed information using IMDb IDs or titles, and fetch poster images.
It wraps the OmdbApi and returns fully hydrated models using the OmdbModels package.
TLDR
use Zerotoprod\Omdb\Omdb; $Omdb = Omdb::from('apiKey'); // Get the poster art of a title by its ImdbID $Omdb->poster('tt0499549'); // https://img.omdbapi.com/?apikey=8f8423aa&i=tt0499549 // Find a title by ImdbID (Internet Movie DataBase ID) or title $Omdb->byIdOrTitle('Avatar')->Title; // 2009 // Find multiple titles $Omdb->search('Avatar')->Search['tt0499549']->Year; // 2009
Requirements
- PHP 8.1 or higher.
- cURL extension enabled (typically enabled by default in most PHP installations).
- A valid OMDb API key. A free key is typically available.
Getting an OMDb API Key
- Go to the OMDb API website.
- Sign up for a free or paid plan depending on your usage requirements.
- After registering, you will receive an API Key that you must pass to the OmdbApi class during initialization.
Installation
Install Zerotoprod\Omdb
via Composer:
composer require zero-to-prod/omdb
This will add the package to your project’s dependencies and create an autoloader entry for it.
Usage
Initialization:
use Zerotoprod\Omdb\Omdb; $Omdb = Omdb::from('apiKey');
You can also customize the base URL and the poster URL if you need to (for example, to proxy through another service):
use Zerotoprod\Omdb\Omdb; $Omdb = Omdb::from( apikey: 'apiKey', base_url: 'https://www.omdbapi.com/', img_url: 'https://img.omdbapi.com/' );
poster()
Get the poster art of a title by its ImdbID
$Omdb->poster('tt0499549'); // https://img.omdbapi.com/?apikey=8f8423aa&i=tt0499549
byIdOrTitle()
Find a title by ImdbID (Internet Movie DataBase ID) or title.
Returns a Title DataModel.
$Omdb->byIdOrTitle('Avatar')->Title; // 2009
search()
Find multiple titles. Note the imdbID
value is used as the key for each movie.
Returns a SearchResults DataModel.
$Omdb->search('Avatar')->Search['tt0499549']->Year; // 2009
Factories
You can use the provided factories without going through the api like this:
\Zerotoprod\OmdbModels\Factories\TitleFactory::factory()->setTitle('Avatar')->make();
Mocking
You can mock the api by implementing the Zerotoprod\OmdbApi\OmdbApiInterface:
use Zerotoprod\OmdbApi\OmdbApiInterface; use Zerotoprod\Omdb\Omdb; class OmdbApiFake implements OmdbApiInterface { public function search() } $Omdb = new Omdb(new OmdbApiFake()); $Omdb->search('Avatar');
Contributing
Contributions, issues, and feature requests are welcome! Feel free to check the issues page if you want to contribute.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Commit changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature-branch
). - Create a new Pull Request.