Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jleagle committed Apr 6, 2015
0 parents commit 71c8c3a
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/vendor
composer.lock
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# omdb-imdb-api-client

A package to retrieve movies and TV information from IMDB using the API at omdbapi.com

Retrieve full movie details, if you know the name or ID of the movie:

```php
$movie = Imdb::retrieve('the matrix');

// If you need to be more specific:
// This will also return Rotten Tomatoes ratings & a longer plot
$movie = Imdb::retrieve('the matrix', Imdb::TYPE_MOVIE, 1999, true, true);
```

Search for a movie:

```php
$movies = Imdb::search('the matrix');

// If you need to be more specific:
$movies = Imdb::search('the matrix', Imdb::TYPE_MOVIE, 1999);
```
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "jleagle/omdb-imdb-api-client",
"keywords": [
"imdb",
"omdb",
"api",
"client",
"helper"
],
"description": "A package to retrieve movies and TV information from IMDB using the API at omdbapi.com",
"license": "MIT",
"authors": [
{
"name": "James Eagle",
"email": "jimeagle@gmail.com",
"homepage": "http://jimeagle.com"
}
],
"require": {
"php": ">=5.4",
"guzzlehttp/guzzle": "~5.0"
},
"require-dev": {
"phpunit/phpunit": "~4.6"
},
"autoload": {
"psr-4": {
"Jleagle\\Imdb\\": "src/"
}
}
}
6 changes: 6 additions & 0 deletions src/Exceptions/ImdbException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Jleagle\Imdb\Exceptions;

class ImdbException extends \Exception
{
}
148 changes: 148 additions & 0 deletions src/Imdb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
namespace Jleagle\Imdb;

use GuzzleHttp\Client;
use Jleagle\Imdb\Exceptions\ImdbException;
use Jleagle\Imdb\Responses\Movie;
use Jleagle\Imdb\Responses\Result;

class Imdb
{
const TYPE_MOVIE = 'movie';
const TYPE_SERIES = 'series';
const TYPE_EPISODE = 'episode';

private function __construct()
{
}

/**
* @param string $movie
* @param string $type
* @param int $year
* @param bool $tomatoes
* @param bool $longPlot
*
* @return Movie
*
* @throws ImdbException
*/
public static function retrieve(
$movie, $type = null, $year = null, $tomatoes = false, $longPlot = false
)
{
$params = [
'type' => $type,
'y' => $year,
'plot' => $longPlot ? 'full' : 'short',
'tomatoes' => $tomatoes ? 'true' : 'false',
];

if(self::isValidId($movie))
{
$params['i'] = $movie;
}
else
{
$params['t'] = $movie;
}

$data = self::_get($params);

return new Movie(
[
'title' => $data['Title'],
'year' => $data['Year'],
'rated' => $data['Rated'],
'released' => $data['Released'],
'runtime' => $data['Runtime'],
'genre' => $data['Genre'],
'director' => $data['Director'],
'writer' => $data['Writer'],
'actors' => $data['Actors'],
'plot' => $data['Plot'],
'language' => $data['Language'],
'country' => $data['Country'],
'awards' => $data['Awards'],
'poster' => $data['Poster'],
'metascore' => $data['Metascore'],
'imdbRating' => $data['imdbRating'],
'imdbVotes' => $data['imdbVotes'],
'imdbId' => $data['imdbID'],
'type' => $data['Type'],
'response' => $data['Response'],
]
);
}

/**
* @param string $search
* @param string $movieType
* @param string $year
*
* @return Result[]
*
* @throws ImdbException
*/
public static function search($search, $movieType = null, $year = null)
{
$data = self::_get(
[
's' => $search,
'type' => $movieType,
'y' => $year
]
);

$return = [];
foreach($data['Search'] as $result)
{
$return[] = new Result(
[
'title' => $result['Title'],
'year' => $result['Year'],
'imdbId' => $result['imdbID'],
'movieType' => $result['Type']
]
);
}
return $return;
}

/**
* @param $string
*
* @return bool
*/
private static function isValidId($string)
{
return preg_match("/tt\\d{7}/", $string) > 0;
}

/**
* @param array $params
*
* @return array
*
* @throws ImdbException
*/
private static function _get($params)
{
$params = array_filter($params);

$params['r'] = 'json';
$params['v'] = '1';

$client = new Client();
$response = $client
->get('http://www.omdbapi.com/', ['query' => $params])
->json();

if(isset($response['Response']) && $response['Response'] == 'False')
{
throw new ImdbException($response['Error']);
}

return $response;
}
}
18 changes: 18 additions & 0 deletions src/Responses/AbstractResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Jleagle\Imdb\Responses;

class AbstractResponse
{
public function __construct(array $data)
{
foreach($data as $field => $value)
{
$this->$field = $value;
}
}

public function toArray()
{
return get_object_vars($this);
}
}
26 changes: 26 additions & 0 deletions src/Responses/Movie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Jleagle\Imdb\Responses;

class Movie extends AbstractResponse
{
public $title;
public $year;
public $rated;
public $released;
public $runtime;
public $genre;
public $director;
public $writer;
public $actors;
public $plot;
public $language;
public $country;
public $awards;
public $poster;
public $metascore;
public $imdbRating;
public $imdbVotes;
public $imdbId;
public $type;
public $response;
}
10 changes: 10 additions & 0 deletions src/Responses/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Jleagle\Imdb\Responses;

class Result extends AbstractResponse
{
public $title;
public $year;
public $imdbId;
public $movieType;
}

0 comments on commit 71c8c3a

Please sign in to comment.