MovieSearch

The first thing you always need to do when you want to use the API is to include the class file. This can be done with one of the four commands:

<?php
include("imdb.class.php");
include_once("imdb.class.php");
require("imdb.class.php");
require_once("imdb.class.php");
?>

For a search with IMDB you need to include imdb.class.php and imdbsearch.class.php whis way - for Moviepilot it is pilot.class.php and pilotsearch.class.php.

Now, how you can search for a movie? You either have the name (or parts of it), or you already have the IMDB ID of the movie. In the latter case, you don't need to perform a search for the movie, but go directly to the MovieDetails. Consider the MovieSearch as a search for the IMDB ID - that is what it does: Listing up IMDB IDs matching to the (partially) movie name you specified.

Submit a search

To describe the necessary steps, let's take an example searching for the movie "The Whole Nine Yards". Assume we don't know the exact name, but just remember the "Nine Yards", this is what it would look like:

<?php
$name = "nine yards";                 // the name will usually be dynamically set
require_once("imdb.class.php");       // include the class file - for Moviepilot: pilot.class.php
require_once("imdbsearch.class.php"); // for Moviepilot: pilotsearch.class.php
$search = new imdbsearch();           // create an instance of the search class. For Moviepilot: $search = new pilotsearch();
$search->setsearchname($name);        // tell the class what to search for (case insensitive)
$results = $search->results();        // submit the search
?>

Parse results

Now the search was submitted, and you can go to parse the results stored in the array $results. As the API reference describes, this is an array[0..n] of objects, where each object is an instance of the class imdb as described in MovieDetails - so basically you could go on and retrieve all movie details for all results now. Which generally is not that useful, since you do not yet know which of these are interesting for you. So normally you want to retrieve some information to identify the movie in question, so you will loop over the $results array e.g. as follows:

<?php
foreach ($results as $res) {
  $mid  = $res->imdbid();
  $name = $res->title();
  $year = $res->year();
  // now do something with these data
  echo "$mid: $name ($year)<br>\n";
}
?>

From the list created in that loop, the user then can select the IMDB ID of the movie in question. You could e.g. provide each item of this list with a link to the details page - "http://".$search->imdbsite."/title/tt".$res->imdbid() would provide a link to the movies page at the IMDB site, 'http://'.$search->pilotsite.'/movies/imdb-id-'.(int)$res->imdbid() is the corresponding Moviepilot link.

With the IMDB ID known, you can proceed to retrieve the details as described on the MovieDetails page.

Last modified by izzy, 04/26/10 20:24:07 (3 months ago)