Table of Contents
As with the MovieSearch, you first must include the class file before using it. To retrieve movie details, you will need to provide the movies IMDB ID, which can be retrieved by a MovieSearch or by copy-and-paste from the IMDB page. However, this wiki page assumes you already know the IMDB ID.
Now, the IMDB sites provide a lot of details for a movie, which are spread over multiple pages. The according page will be retrieved on the first request for it (i.e. when you tried to retrieve data from that page), and kept in memory for subsequent accesses. If you have the cache enabled, it will even be stored for future "sessions", to give a better performance.
So let's assume you request the title, year, runtime, rating and a list of trailers, this would look as follows:
<?php require_once("imdb.class.php"); // include the class file. For Moviepilot: pilot.class.php $movie = new imdb($mid); // create an instance of the class and pass it the IMDB ID // For Moviepilot: new pilot($mid) $title = $movie->title(); // retrieve the movie title $year = $movie->year(); // obtain the year of production $runtime = $movie->runtime(); // runtime in minutes $rating = $movie->mpaa(); // array[country=>rating] of ratings $trailer = $movie->trailers(); // array of trailers ?>
Going in a loop for multiple movies, you can simply reset the class using the $movie->setid($newid) method – no need to drop and recreate the class instance. For more details on available methods, and the structure of their returned values, please refer to the API documentation.
For those interested in, here comes a short description of what would happen at each of the steps described above:
|require_once("imdb.class.php");||The class file will be included into your current script. This implicitly includes all classes it depends upon, such as the mdb_base.class.php.|
|new imdb($mid);||This will create a new instance of the imdb class, and set the IMDB ID for the movie to process at the same time.|
|$movie->title();||The movies title page will be accessed. Since it was not retrieved before, this will result in requesting the page from the configured IMDB site. If caching is enabled, the page will be stored in the cache. The page will for sure be held in a class variable. This variables content will be parsed for the movie title, which is then also kept in another variable – and returned to the calling process.|
|$movie->year();||The movies title page will be accessed again. Since it is already found in the internal variable, the latter will be parsed for the year – which again is stored internally and returned to the calling process.|
|$movie->year();||Assumed you call this a second time, it will simply return the stored value.|
|$movie->runtime();||Same as with the first call to $movie->year().|
|$movie->mpaa();||Again the same.|
|$movie->trailers();||This will access a new page, and thus be processed as $movie->title() above.|
This example shows that we try to access external pages as little as possible. Processing the same page with the same IMDB ID again would access the pages from the cache (if enabled) instead of retrieving them again from the IMDB site. Moreover, we also only retrieve the pages needed for our requests – so in our above example, e.g. the /fullcredits page is not even touched since none of its details have been asked for. This is the best we can do to increase the performance. If you, however, go to retrieve all possible details, the API needs to retrieve (and parse) all pages necessary – which may (and probably will) cause a delay for data delivery. So you better don't put all details on one page.