Skip to content

Latest commit

 

History

History
224 lines (156 loc) · 7.25 KB

README.md

File metadata and controls

224 lines (156 loc) · 7.25 KB

A PHP Wrapper for use with the TMDB API.

Build Status Develop Branch Code Coverage Scrutinizer Quality Score License

Inspired by php-github-api, php-gitlab-api and the Symfony2 Community.

If you have any questions or feature requests, please visit the google+ community.

Stable

Latest Stable Version Latest Unstable Version Dependency Status

Currently unit tests are run on travis, with the following versions:

  • 5.3.3
  • 5.3
  • 5.4
  • 5.5
  • 5.6
  • hhvm ( we do not officially support this, however tests indicate it should be functional. )

Help & Donate

If you use this in a project whether personal or business, I'd like to know where it is being used, so please drop me an e-mail :-)!

If this project saved you a bunch of work, or you just simply appreciate my efforts, please consider donating a beer ( or two ;) )!

Features

  • An complete integration of TMDB ( accounts, movies, tv etc. if something is missing I haven't added the updates yet! ).
  • Array implementation of the movie database
  • Model implentation of the movie database trying to follow their model as closely as possible.
  • An ImageHelper class to help build image urls or html elements.
  • BackoffRetry plugin enabled by default to handle any rate limit errors.
  • Caching support based on max-age headers returned by TMDB, requires doctrine-cache.
  • Logging support, requires monolog.

Plug-ins

Installation

Install Composer

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

Add the following to your require block in composer.json config

"wtfzdotnet/php-tmdb-api": ">=1.2.0"

Include Composer's autoloader:

require_once dirname(__DIR__).'/vendor/autoload.php';

New to composer?

Just create a file named composer.json in your document root:

{
    "require": {
        "wtfzdotnet/php-tmdb-api": ">=1.2.0"
    }
}

General API Usage

If your looking for a simple array entry point the API namespace is the place to be.

First we always have to construct the client:

$token  = new \Tmdb\ApiToken('your_tmdb_api_key_here');
$client = new \Tmdb\Client($token);

Or if you prefer requests to happen securely:

$client = new \Tmdb\Client($token, null, true);

If you want to add some caching capabilities ( currently an implementation of the GuzzleCachePlugin );

$client->setCaching(true, '/tmp/php-tmdb-api');

This relies on max-age headers being sent back from TMDB, see the documentation of the CachePlugin.

If you want to add some logging capabilities ( currently an implementation of the GuzzleLogPlugin, requires monolog/monolog);

$client->setLogging(true, '/tmp/php-tmdb-api.log');

Then we do some work on it:

$movie = $client->getMoviesApi()->getMovie(550);

If you want to provide any other query arguments.

$movie = $client->getMoviesApi()->getMovie(550, array('language' => 'en'));

Model Usage

However the library can also be used in an object oriented manner.

First we always have to construct the client:

$token  = new \Tmdb\ApiToken('your_tmdb_api_key_here');
$client = new \Tmdb\Client($token);

Or if you prefer requests to happen securely:

$client = new \Tmdb\Client($token, null, true);

If you want to add some caching capabilities ( currently an implementation of the GuzzleCachePlugin );

$client->setCaching(true, '/tmp/php-tmdb-api');

This relies on max-age headers being sent back from TMDB, see the documentation of the CachePlugin.

If you want to add some logging capabilities ( currently an implementation of the GuzzleLogPlugin, requires monolog/monolog);

$client->setLogging(true, '/tmp/php-tmdb-api.log');

Then you pass this client onto one of the many repositories and do some work on it.

$repository = new \Tmdb\Repository\MovieRepository($client);
$movie      = $repository->load(87421);

echo $movie->getTitle();

The repositories also contain the other API methods that are available through the API namespace.

$repository = new \Tmdb\Repository\MovieRepository($client);
$topRated = $repository->getTopRated(array('page' => 3));
// or
$popular = $repository->getPopular();

An ImageHelper class is provided to take care of the images, which does require the configuration to be loaded:

$configRepository = new \Tmdb\Repository\ConfigurationRepository($client);
$config = $configRepository->load();

$imageHelper = new \Tmdb\Helper\ImageHelper($config);

echo $imageHelper->getHtml($image, 'w154', 154, 80);

We also provide some easy methods to filter any collection, you should note however you can always implement your own filter easily by using Closures:

foreach($movie->getImages()->filter(
        function($key, $value){
            if ($value instanceof \Tmdb\Model\Image\PosterImage) { return true; }
        }
    ) as $image) {

    // do something with all poster images
}

These basic filters however are already covered in the Images collection object:

$backdrop = $movie
    ->getImages()
    ->filterBackdrops()
;

And there are more Collections which provide filters, but you will find those out along the way.

Some other useful hints

There are 2 types of "main" collections, the GenericCollection and the ResultCollection.

The `GenericCollection holds any collection of objects ( e.g. an collection of movies ).

The ResultCollection is an extension of the GenericCollection, and inherits response parameters ( page, total_pages, total_results ) from an resultset, this can be used to create paginators.