• Technical
  • How to fetch movie identifier/id from IMDB using PHP

How to fetch movie identifier/id from IMDB using PHP

Hi Guys

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 – http://blogs.niteshapte.com/2013-06-21-how-to-fetch-trailers-from-youtube-using-php.htm.

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,

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)) {                        
                        header('Location: http://www.imdb.com/title/'.$matches[1].'');
                        exit();		
		}
		else if(@preg_match('~<td class="result_text">.*?href="/title\/(.*?)".*?</td>~s', $page, $matches)) {
			header('Location: http://www.imdb.com/title/'.$matches[1].''); 
                        exit();
		}
		else {
			header('Location: http://www.imdb.com/find?s=all&q='.$movieName.' ('.$year.')');
			exit();
		}
	}
}
?>

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 code: HERE

That’s all folks.

Hope above class will be helpful to a lot of developers who were looking for such script.

Suggestion/critics are very much welcome.

Have a nice day ahead.

15,343 total views, 11 views today

This entry was posted in Technical

2 Responses

  • Awesome blog you have here but I was curious if you knew of any discussion
    boards that cover the same topics talked about in this
    article? I’d really like to be a part of online community where I can get advice from other experienced people that share the same interest. If you have any recommendations, please let me know. Thanks!

    Reply


Leave a Reply

Your email address will not be published. Required fields are marked *


× eight = 24

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Follow Me