Recent Articles

IMDB API implementation to get movie information

IMDb (Internet Movie Database) is the biggest database of movie information on Internet. IMdb has over 2 million titles of movies and episodes. IMDb is a reliable source for movie information to be used for web applications related to movie and television episodes. Unfortunately IMDb has not published APIs to get the movie information programatically. But fortunately there exists a third party API source (omdbapi.com) from which IMDb movie information can be fetched programatically and used in our applications. The OMDb (Open Movie Database) APIs were developer by Brian Fritz and are widely used to get IMDb movie information.

The following code demonstrate how any movie information can be fetched from IMDb using omdb api.

Important : The following code uses ‘file_get_contents’ function of PHP to get the movie information from omdbapi.com, so its important to ensure that ‘allow_url_fopen’ variable is On in php.ini

API Url : http://www.omdbapi.com/?t=$title&y=$year

Response : The sample response looks like this

The API url has many parameters that can be passed to get corresponding result about a movie. The parameters are mentioned below.

omdb api parameters
 

Code to get the movie information


<?php 
//Get the title of the movie
$title = "Mr. Bean's Holiday";

//'y'(Year) key at the end of the url is optional.
//But its always good practice to sent the year; 
//as title can be same for multiple movies
$year = "2007";

//Replace spaces and apostrophe mark in the title with html entities
//This will make title from 'Mr. Bean's Holiday' 
//to 
//'Mr.%20Bean%27s%20Holiday'
$title = urlencode($title);

//Call the omdb api
$json=file_get_contents("http://www.omdbapi.com/?t=$title&y=$year");

$details=json_decode($json);

//Check if respose contains the movie information
if($details->Response=='True')
{   
    //Print the movie information
    echo "IMDB-ID : ".$details->imdbID.'<br>';
    echo "Title : ".$details->Title.'<br>';
    echo "Year : ".$details->Year.'<br>';
    echo "Rated : ".$details->Rated.'<br>';
    echo "Poster Image Path: ".$details->Poster.'<br>';
    echo "<img src=\"$details->Poster\"><br>";
    echo "Released Date: ".$details->Released.'<br>';
    echo "Runtime : ".$details->Runtime.'<br>';
    echo "Genre : ".$details->Genre.'<br>';
    echo "Director : ".$details->Director.'<br>';
    echo "Writer : ".$details->Writer.'<br>';
    echo "Actors : ".$details->Actors.'<br>';
    echo "Plot : ".$details->Plot.'<br>';
    echo "Language : ".$details->Language.'<br>';
    echo "Country : ".$details->Country.'<br>';
    echo "Awards : ".$details->Awards.'<br>';
    echo "Metascore : ".$details->Metascore.'<br>';
    echo "IMDB Rating : ".$details->imdbRating.'<br>';
    echo "IMDB Votes : ".$details->imdbVotes.'<br>';
}
//Show message if the movie information is not returned by APIs
else 
{
     echo "Movie information not available.Please confirm title";
}
?>

E.g. : Let us retrieve one of my favourite movie ‘Mr. Bean’s Holiday’ information from IMDb.

So the API url will be

$json=file_get_contents("http://www.omdbapi.com/?t=Mr. Bean's Holiday");

The formatted desired response received using the above code can be checked here.

Copy above code, paste it in your editor and you are set to get any movie information from IMDb.

  • mohammad

    this is awesome ! but, how about “s” parameter. how i can display imdbID’s in result page?
    can you Explanation that with XML callback ?