Author: Nitesh Apte
Posted on: 2013-07-07
Package: IMDB
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.
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.
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. You can find more post in my blog. http://blogs.niteshapte.com.
Have a nice day ahead.
You need to be a registered user or login to post a comment
1,344,285 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
No comments were submitted yet.