You are hereBlogs / alif's blog / IMDB Details Grabber: Using PHP DOM XPath to extract Movie details
IMDB Details Grabber: Using PHP DOM XPath to extract Movie details
Recently, a friend of mine asked me if there's an IMDB Parser that allows to fetch information about any Movie on IMDB. Unfortunately, IMDB does not offer an API, so , after doing a bit googling I found nice IMDB Grabber (PHP IMDB Grabber), despite it being a good one, I decided develop my own class relying on XPath and Dom Traversing.
Brief Explanation of Methods:
The Class 'class.imdb.php' has 3 useful Methods (in addition other ones). They are:
| get($url) | Fetches the contents of a specific Movie on IMDB. Call this Method by passing the URL and this Method would return as associate array containing the title, image, url, Plot, Director, Release Date and Runtime. |
| useCSV($status) | By Default, when get Method is invoked, values returned are in CSV format. If this Method is invoked with false value as parameter, the values returned from get($url) Method would be a list of array, For example: if a Movie has multiple Directors, by default, it when get($url) Method is invoked, the value of 'Director:' would be a CSV containing multiple Director Names. If useCSV(false) is invoked and then get($url) is called, the associative value of 'Director:' would be an array containing list of Directors. Please see Usage for more info. |
| showCast($t) | The parameter is a boolean. If you wish to grab the cast (some people have requested it), then call this method with a 'true' parameter. Then, when you grab an imdb page, the returned value would also contain the cast in the movies with their thumb image, real name and cast name. |
| showRating($t) | The parameter is a boolean. If you wish not to grab the rating, then call this method with a 'false' parameter. |
Usage (Basic):
Using the class is very easy. Include the class file, initialize the class and then invoke get($url), where $url is the IMDB URL for movie.
include 'path/to/class/class.imdb.php';
$imdbObj = new Imdb();
// movieInfo contains the details of Movies in associative array format
$movieInfo = $imdbObj->get('http://www.imdb.com/title/tt0167260/');
// OR simply enter the movie name :)
$movieInfo = $imdbObj->get('The Matrix');
Here's a vardump of movieInfo:
Array ( [title:] => The Lord of the Rings: The Return of the King (2003) [image:] => http://ia.media-imdb.com/images/M/MV5BMjE4MjA1NTAyMV5BMl5BanBnXkFtZTcwNzM1NDQyMQ@@._V1._SX94_SY140_.jpg [url:] => http://www.imdb.com/title/tt0167260/ [Plot:] => The former Fellowship of the Ring prepare for the final battle for Middle Earth, while Frodo & Sam approach Mount Doom to destroy the One Ring.| [Director:] => Peter Jackson [Release Date:] => 17 December 2003 (USA) [Runtime:] => 201 min | 251 min (extended edition) )
That's it, simple enough!. By Default, title, image, url, Plot, Director, Release Date and Runtime is returned. All Keys of the associative array as suffixed with ':' (colon), I leave it upto developers to either strip it out, or just display it.
Usage (Customization and Advanced Usage):
By default, the class fetches the title, image, url, Plot, Director, Release Date and Runtime, Aspect Ratio, Writers, from IMDB.
If you wish to grab the cast in the movie, then call the following method:
$imdbObj->showCast(true);
CSV: By Default, all Values returned are in CSV Format, if you want to get value as list of array, for example for Writers, you way want to get the list of Writers as in Array instead of CSV. In that case do the following:
$imdbObj = new Imdb();
$movieInfo = $imdbObj->showCast(true)
->useCSV(false)
->get('http://www.imdb.com/title/tt0167260/');
View Demo Download from Github
| Attachment | Size |
|---|---|
| test.imdb.php.txt | 642 bytes |
| class.imdb.php.txt | 5.87 KB |
| demo.php_.txt | 2.13 KB |
Thats strange...I just tried it locally and it seems to work for me. Can you please explain what error you are getting?. Are you getting no result (just blank) or something else.
Thanks,
$IMDB emtpy when $Title is "The Italian Job (1969)", but
is filled when "The Italian Job"
$ErrorLevel = error_reporting(E_ERROR);
$imdbObj = new Imdb();
$IMDB = $imdbObj->showCast(true)
->add('Genre:', '/div/a')
->add('Tagline:', '/div/text()')
->add('Certification:', '/div/a')
->get($Title);
error_reporting($ErrorLevel);
echo "IMDB(".$Title.")";
echo '';
print_r($IMDB);
echo '';
I tried the code and it seems to be working for me. So, I am not sure why you are getting that error :| :S
This is what I tried (same as yours):
$Title = 'The Italian Job (1969)';
$IMDB = $imdbObj->showCast(true)
->add('Genre:', '/div/a')
->add('Tagline:', '/div/text()')
->add('Certification:', '/div/a')
->get($Title);
echo "
Fatal error (this could be when the IMDB query fails)
PHP Fatal error: Call to a member function getAttribute() on a non-object
public function getImdbURL($url) {
$searchURL = 'http://www.imdb.com/find?q='.urlencode($url);
$searchDom = new DomDocument();
$searchLoad = $searchDom->loadHTMLFile($searchURL);
$xpath = new DomXPath($searchDom);
return 'http://www.imdb.com'.$xpath->query('//p[@style]/b/a')->item(0)->getAttribute('href');
}
Thanks for the catch.
I noticed it throws an error when an invalid search term (say: 'x98359835') is entered. In which case, Imdb doesn't return any valid Movie.
The error also occurs if you enter an invalid url (say 'http://www.google.ca/'), which isn't a valid imdb url.
Both the issues should be fixed now. Please download the updated files (class.imdb.php.txt and test.imdb.php.txt).
Thanks,
Well done - a failed search (eg, "Shutter Island") now returns no results and not a FATAL error reported by PHP.
The search term is valid, it's just that no results are obtained.
Thanks
I just noticed the error. It seems for some search results, Imdb simply redirects to the Movie URL without showing any search Result. For others, Imdb returns a listing of Movies as strong match.
For example: If you search 'The Matrix'.
http://www.imdb.com/find?s=all&q=Matrix
Imdb will return a search page with the Search Result.
Whereas, if you type 'Shutter Island', if simply redirects to the movie page without displaying the results.
http://www.imdb.com/find?s=all&q=Shutter+Island
Redirects to: http://www.imdb.com/title/tt1130884/
without showing any search Result. Thanks for pointing the issue. I have fixed it for now.
If there are any other issues you have come across, let me know,
Thanks,
This is an great piece of script. Its much easier to use.
Hey Alif,
thanks for this nice script (-:
but how can i show the results in my Page with HTML?
like "echo "Film: " + $movieInfo[????];
I m not the biggest Profi (-: Sorry..
wou8ld nice if you help me (-:
Thanks, Dennis
Hi Dennis,
If you want to display the infos in php pages, then you can use:
Film: =$movieInfo['title:']?>
Plot: =$movieInfo['Plot:']?>
Director: =$movieInfo['Director:']?>
...etc. and so on (if you use the above syntax, make sure short_tags are turned on in your php settings).
You can view all the associated array keys in the test.imdb.php class.
I hope it helps,
why dont this wor
$movieInfo = $imdbObj
->add('Archive Footage:', '/div/a')
->get('http://www.imdb.com/name/nm0004710/');
http://www.imdb.com/name/nm1200692/
Hello, It won't work for Grabbing Celebrity details from a Celebrity page. This Grabber works for Grabbing Movie Details of a Movie. For example, it will work for:
http://www.imdb.com/title/tt0320661/
Or even if you type 'Kingdom of Heaven', that should also work (for the above result).
any chance you can get celebrity details eg from http://www.imdb.com/name/nm1200692/??
Hello Alif,
Thank you. I was looking for information on how to do this myself when I stumbled on your code. This is directly usable. Nice work.
I have one question. I would also like to grab the "User Rating:" but I have trouble locating the text.
Is this possible to do at all?
Thanks in advance,
EJEE
Yes, its definitely possible. Infact, I just updated my class to reflect it. Now, the class should return User Rating and Total Votes by default. But, you can turn it off by calling the Method showRating(false).
Thanks,
Nice script...
Its simple and easy to use. Thanks for sharing.
Is there anyway cast can be grabbed?. I am looking at this script and but I need a way to grab the cast. Is it possible?
Yes, I have added the ability to grab cast now :)
Thanks for making this. I have been looking for something like this to manage my persona movie collection using PHP / MySQL.
When I put your 2 files in my xampp/htdocs/moviecritc folder and run it through http://localhost/moviecritc/test.imdb.php in the browser, I get the following error:
http://i.imgur.com/YGRxG.png
Can you please help identify this? Thanks.
This piece of writing is excellent and I liked it a lot.
This is really awesome. Really easy to use.
Thanks
Hi there
i have used your script. it works great. but whatever i do i cant retrieve the CAST of the movie from that page. Please let me know if its possible
cheers
I think yours will be even better than these alternatives. Looks pretty straight forward.
The code doesn't seem to work, I get error messages, it cannot find DomDocument ? Where is that declared..???
Can you please explain what error you are getting. DomDocument is the standard Class of PHP-5. Ensure that you are using PHP-5.1 atleast.
For some reason, I'm getting the following error:
Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in http://www.imdb.com/title/tt0167260/, line: 642 in class.imdb.php on line 146
Over and over and over again, but the result does show at the end.
Thanks for trying it out. From the messages on your comment, it seems to be a warning, not an error. I will look into it. For the time being, if you wish the warning message not to be displayed, you can use error_reporting feature of php to not show the warning.
http://us2.php.net/manual/en/function.error-reporting.php
thanks :-)
i'll try to use it soon...
Post new comment