tiagosampaio / tmdb-api-php
PHP wrapper for TMDB (TheMovieDatabase) API v3. Supports two types of approaches, one modelled with repositories, models and factories. And the other by simple array access to RAW data from The Movie Database.
Requires
- php: >=5.6.0
- ext-curl: *
- doctrine/cache: ^1.6
- guzzlehttp/guzzle: ~6.0
- kevinrob/guzzle-cache-middleware: ^1.2 || ^2.0
- psr/log: ~1.0
- rtheunissen/guzzle-log-middleware: ^0.4.0
- symfony/event-dispatcher: >=2.4,<5
- symfony/options-resolver: >=2.6,<5
Requires (Dev)
- monolog/monolog: >=1.11.0
- phpunit/phpunit: >=5.7,<6
Suggests
- monolog/monolog: Required if you want to make use of logging features.
- 3.0.x-dev
- 3.0.0
- 2.1.x-dev
- dev-master / 2.1.x-dev
- 2.1.17
- 2.1.16
- v2.1.15
- v2.1.14
- v2.1.13
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- 2.1.4
- v2.1.3
- 2.1.2
- 2.1.0
- 2.0.x-dev
- v2.0.19
- v2.0.18
- v2.0.17
- v2.0.16
- v2.0.15
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.0
- v1.0.0-BETA1
- v1.0.0-ALPHA5
- v1.0.0-ALPHA4
- v1.0.0-ALPHA3
- v1.0.0-ALPHA2
- v1.0.0-ALPHA1
This package is auto-updated.
Last update: 2024-12-08 06:53:20 UTC
README
Inspired by php-github-api, php-gitlab-api and the Symfony Community.
If you have any questions or feature requests, please visit the google+ community.
Stable
Currently unit tests are run on travis, with the following versions:
- 5.6
- 7.0
- 7.1
- HHVM (failures allowed)
- nightly (failures allowed)
Features
Main features
- An complete integration of all the TMDB API has to offer (accounts, movies, tv etc. if something is missing I haven't added the updates yet!).
- Array implementation of the movie database (RAW)
- Model implementation of the movie database (By making use of the repositories)
- An
ImageHelper
class to help build image urls or html elements.
Other things worth mentioning
- Retry subscriber enabled by default to handle any rate limit errors.
- Caching subscriber enabled by default, based on
max-age
headers returned by TMDB, requiresdoctrine-cache
. - Logging subscriber and is optional, requires
monolog
. Could prove useful during development.
Plug-ins
- Symfony
- Laravel
Installation
Install Composer
$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
You are not obliged to move the composer.phar
file to your /usr/local/bin
, it is however considered easy to have an global installation.
Add the following to your require block in composer.json
config
"php-tmdb/api": "~2.1"
If your new to composer and have no clue what I'm talking about
Just create a file named composer.json
in your document root:
{ "require": { "php-tmdb/api": "~2.1" } }
Now let's install and pull in the dependencies!
composer install
Include Composer's autoloader:
require_once dirname(__DIR__).'/vendor/autoload.php';
To use the examples provided, copy the apikey.php.dist
to apikey.php
and change the settings.
Constructing the Client
First we always have to construct the client:
$token = new \Tmdb\ApiToken('your_tmdb_api_key_here'); $client = new \Tmdb\Client($token);
If you'd like to make unsecure requests (by default we use secure requests).
$client = new \Tmdb\Client($token, ['secure' => false]);
Caching is enabled by default, and uses a slow filesystem handler, which you can either:
- Replace the
path
of the storage of, by supplying the option in the client:
$client = new \Tmdb\Client($token, [ 'cache' => [ 'path' => '/tmp/php-tmdb' ] ]);
- Or replace the whole implementation with another CacheStorage of Doctrine:
use Doctrine\Common\Cache\ArrayCache; $client = new \Tmdb\Client($token, [ 'cache' => [ 'handler' => new ArrayCache() ] ] );
This will only keep cache in memory during the length of the request, see the documentation of Doctrine Cache for the available adapters.
Strongly against this, disabling cache:
$client = new \Tmdb\Client($token, [ 'cache' => [ 'enabled' => false ] ]);
If you want to add some logging capabilities (requires monolog/monolog
), defaulting to the filesystem;
$client = new \Tmdb\Client($token, [ 'log' => [ 'enabled' => true, 'path' => '/var/www/php-tmdb-api.log' ] ]);
However during development you might like some console magic like ChromePHP
or FirePHP
;
$client = new \Tmdb\Client($token, [ 'log' => [ 'enabled' => true, 'handler' => new \Monolog\Handler\ChromePHPHandler() ] ]);
General API Usage
If your looking for a simple array entry point the API namespace is the place to be.
$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, which I reckon is the preferred way of doing things.
Instead of calling upon the client, you pass the client onto one of the many repositories and do then 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();
Some other useful hints
Event Dispatching
Since 2.0 requests are handled by the EventDispatcher
, which gives you before and after hooks, the before hook allows an event to stop propagation for the
request event, meaning you are able to stop the main request from happening, you will have to set a Response
object in that event though.
See the files for TmdbEvents and RequestSubscriber respectively.
Image Helper
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);
Plug-ins
At the moment there are only two useful plug-ins that are not enabled by default, and you might want to use these:
$plugin = new \Tmdb\HttpClient\Plugin\LanguageFilterPlugin('nl');
Tries to fetch everything it can in Dutch.
$plugin = new \Tmdb\HttpClient\Plugin\AdultFilterPlugin(true);
We like naughty results, if configured this way, provide false
to filter these out.
Collection Filtering
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.
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 the response parameters (page, total_pages, total_results) from an result set,
this can be used to create paginators.
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 ;))!