A PHP wrapper for the OMDb API with full object support

v1.0.4 2025-01-12 18:00 UTC

This package is auto-updated.

Last update: 2025-01-23 14:17:02 UTC


README

Repo GitHub Actions Workflow Status Packagist Downloads Packagist Version License wakatime Hits-of-Code

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

  1. Go to the OMDb API website.
  2. Sign up for a free or paid plan depending on your usage requirements.
  3. 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.

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Commit changes (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new Pull Request.