PHP Classes

How to Find IMDB ID using PHP to Get IMDB ID from Movie Title - Get the IMDB Movie ID List from the Movie Identifier - IMDB package blog

Recommend this page to a friend!
  All package blogs All package blogs   IMDB IMDB   Blog IMDB package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Find IMDB ID u...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 1,292

Last month viewers: 396

Package: IMDB

How to fetch the movie identifier id from IMDB just by using movie name and year of its release.




Loaded Article

How to Fetch the Movie Identifier ID to Get the ID IMDB Value just by Using Movie Name and Year of its Release using the PHP IMDB class

Few days back, I published an article for fetching movie trailers from YouTube based on movie name and year of its release. Hope you guys liked that. In case someone would like to check that out, here is the link.

Today, I am going to show you how to fetch the movie identifier/id from IMDB just by using movie name and year of its release. You can find it on PHPClasses.org too. Here is the link.

You guys must have used several open source packages for fetching details of movie from IMDB but in all those you need to have the unique identifier of movie that IMDB uses. For instance, the IMDB link of movie "Coach Carter" is http://www.imdb.com/title/tt0393162/ and the unique identifier of this movie is tt0393162, so you need to know this identifier. For couple of movies, it is fine to hard code these identifiers but in case the list of movies are in thousands you need some alternate solution. How about getting those identifiers at run time?

So, here is the code that will help you to achieve this. This class fetch the movie identifier and redirect to the IMDB page of that particular movie.

Notice, the variable $matches. $matches[1] store the value of the movie identifier.

Usage is simple:

<?php
 include 'class.IMDBSearch.php';
 IMDBSearch::_movieRedirect( "Coach Carter", "2004");
?>

That’s it.

There are several open source scripts available on internet for fetching a movie detail from IMDB. But in all those you need to hard code the IMDB page link (containing the identifier) of that particular movie. So, you can combine my code and those code to get the complete details of a movie. Wouldn’t be it easy now? ;-)

Also, you can tweak the above class and instead of redirecting fetch the HTML, and parse it and use regular expression to extract the required details.

class.IMDBSearch.php

<?php
 class IMDBSearch {

  public static function _movieRedirect($movie, $year) {
   $movieName = str_replace(' ', '+', $movie);

   $page = @file_get_contents( 'http://www.imdb.com/find?s=all&q='. $movieName. ' ('.$year.')');
   if(@preg_match('~<p style="margin:0 0 0.5em 0;"><b>Media from .*?href="/title\/(.*?)".*?</p>~s', $page, $matches)) {
    $rawData = @file_get_contents( 'http://www.imdb.com/title/'. $matches[1]);
   }
   else if(@preg_match('~<td class="result_text">.*?href="/title\/(.*?)".*?</td>~s', $page, $matches)) {
    $rawData = @file_get_contents( 'http://www.imdb.com/title/'. $matches[1]);
   }
   else {
    $rawData = @file_get_contents( 'http://www.imdb.com/find?s=all&q='. $movieName. ' ('.$year.')');
   }
  }
}
?>

Here, $rawData is that variable which contain the complete HTML of IMDB page of a movie. Parse it and use regular expression to extract the required details.

You can check the demo here. When you are there, click on the [IMDB] text mentioned after a movie title and you will be redirected to IMDB page of that movie.

Download the PHP IMDB Class

That's all folks. I hope above class will be helpful to a lot of developers who were looking for such script.

You can download the complete PHP IMDB class package ZIP archive or install it with the composer tool with instructions on the same page.

Suggestion/critics are very much welcome. You can find more post in my blog. Have a nice day ahead.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   IMDB IMDB   Blog IMDB package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Find IMDB ID u...