Monday 17 August 2015

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.
http://www.omdbapi.com/?t=$title&y=$year
The API url has many parameters that can be passed to get corresponding result about a movie.
The parameters are mentioned below.



Code to get the movie information
<?php 
 //Get the title of the movie
 $title = "Doom 3";

 //'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 = "";

 //Replace spaces and apostrophe mark in the title with html entities
 //This will make title from 'Doom 3' 
 //to 
 //'Doom%203'
 $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')
  { 
                     echo "<b>IMDB-ID </b> : ".$details->imdbID.'<br>';
       echo "<b>Title </b>: ".$details->Title.'<br>';
       echo "<b>Year </b>: ".$details->Year.'<br>';
       echo "<b>Rated </b>: ".$details->Rated.'<br>';
       echo "<b>Poster Image Path</b>: ".$details->Poster.'<br>';
       echo "<img src=\"$details->Poster\"><br>";
       echo "<b>Released Date</b>: ".$details->Released.'<br>';
       echo "<b>Runtime </b>: ".$details->Runtime.'<br>';
       echo "<b>Genre </b>: ".$details->Genre.'<br>';
       echo "<b>Director </b>: ".$details->Director.'<br>';
       echo "<b>Writer </b>: ".$details->Writer.'<br>';
       echo "<b>Actors </b>: ".$details->Actors.'<br>';
       echo "<b>Plot </b>: ".$details->Plot.'<br>';
       echo "<b>Language </b>: ".$details->Language.'<br>';
       echo "<b>Country </b>: ".$details->Country.'<br>';
       echo "<b>Awards </b>: ".$details->Awards.'<br>';
       echo "<b>Metascore </b>: ".$details->Metascore.'<br>';
       echo "<b>IMDB Rating </b> : ".$details->imdbRating.'<br>';
       echo "<b>IMDB Votes </b>: ".$details->imdbVotes.'<br>';
   }
   //Show message if the movie information is not returned by APIs
    else 
      {
        echo "Movie information not available.Please confirm title";
     }
?>

No comments:

Post a Comment