Location: PHPKode > scripts > IMDBSearch > imdbsearch/IMDBSearch.php
<?
/*	IMDBSearch Class - get movie information from IMDB
	Don't modify without written authorization
	Created by: Randy Gerritse
	Email: hide@address.com
	©2003, All rights reserved.
	================================================================================ */

//IMDBSearch PHP Class - API Core
define ('IMDB_IMG_CHECK',           "?id=");

//imdb url
define ('IMDB_CREDITS',             "http://us.imdb.com/Credits?");

//imdb url regexp list -- Do not modify unless you know 
define ('RE_IMDB_TITLE',             "/<title>(.*)<\/title>/i");
define ('RE_IMDB_COVER',             "/<img alt=\"cover\" align=\"left\" src=\"(.*)\" hei/i");
define ('RE_IMDB_RATING',            "/<B>([0-9]\.[0-9])\/10<\/B>/i");
define ('RE_IMDB_GENRE',             "/(<a href=\"\/Sections\/Genres\/.+?>.*)\s<a href=\"\/Keywords/i");
define ('RE_IMDB_TAGLINE',           "/Tagline:<\/b>\s(.*)/i");
define ('RE_IMDB_YEAR',              "/<title>.*\s*\((\d{4})\)<\/title>/i");
define ('RE_IMDB_OUTLINE',           "/Plot Outline:<\/b>(.*)(\<+)*\./i");
define ('RE_IMDB_OUTLINE_MORE',      "/<a href=\"\/Plot\?(\d{7})\">(.*)<\/a>/i");
define ('RE_IMDB_ID',                "/\d{7}/");
define ('RE_IMDB_CAST',              "/<a href=\"\/Name\?([\w\s\.',\/\(\)\-\x80-\xff%]*)\">([\w\s\.',\/\(\)\-\x80-\xff]*)<\/a><\/TD><TD VALIGN=\"TOP\" nowrap=\"1\"> \.\.\.\. <\/td><td valign=\"top\">(?:<A HREF=\"\/Quotes\?\d{7}\">)?([\w\s\.',\/\(\)\-\x80-\xff]*)(?:<\/A>)?<\/TD><\/TR>/i");
define ('RE_IMDB_DIRECTOR',          "/<a href=\"\/Name\?(.*)\">(.*)<\/A>/i");

define ('RE_IMDB_MULTI',             "/<li><a href=\"\/Title\?(\d{7})\">(.*)<\/a>/i");
define ('RE_IMDB_SINGLE',            "/<a href=\"\/Details\?(\d{7})\">combined details<\/a>/i");

class IMDBSearch {

	//fetch imdb page from internet
	function _imdb_fetch_from_net($path, $data) {
		$host = "us.imdb.com";
		$method = "GET";
		$path = "/".$path;
		$data = urlencode($data);
		if (empty($method))
			$method = 'GET';
		$method = strtoupper($method);
		$fp = fsockopen($host,80);
		if ($method == 'GET')
			$path .= '?' . $data;
		fputs($fp, "$method $path HTTP/1.1\n");
		fputs($fp, "Host: $host\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
		fputs($fp, "Content-length: " . strlen($data) . "\n");
		fputs($fp, "User-Agent: MSIE\n");
		fputs($fp, "Connection: close\r\n\r\n");
		if ($method == 'POST')
			fputs($fp, $data);
		while (!feof($fp))
			$buf .= fgets($fp,128);
		fclose($fp);
		return $buf;
	}
	
	function IMDBSearch($imdbid) {
		if ($imdbid) {
			//if user inputs an imdb serial, getting movie page directly
			$query = $imdbid = trim($imdbid);
			//If text entered seems to be an IMDb id (e.g: 0278504)
			if (preg_match(RE_IMDB_ID, $query)) {
				
				if (!preg_match(RE_IMDB_ID, $query))
					$query = urlencode($query);
	            
				//get page from internet
				$this->imdb_result   = $this->_imdb_fetch_from_net("Title",$query) or die("no results");
				$this->imdb_credits  = $this->_imdb_fetch_from_net("Credits",$query) or die("no credits");
				
				//create the return variable
				$rec = "";
				
				$rec["id"] 			= $imdbid;
				$rec["title"]		= $this->imdb_get_title();
				$rec["cover"]		= $this->imdb_get_cover();
				$rec["year"]		= $this->imdb_get_year($this->imdb_result);
				$rec["rating"]		= $this->imdb_get_rating();
				$rec["genre"]		= $this->imdb_get_genre_arr();
				$rec["tagline"]		= $this->imdb_get_tagline();
				$rec["outline"]		= $this->imdb_get_plot_outline();
				$rec["outlinemore"]	= $this->imdb_get_plot_outline_more();
				$rec["cast"]		= $this->imdb_get_cast_arr();
				$rec["directors"]	= $this->imdb_get_directors_arr();
				
				$this->imdb_result = $rec;
				
			} else {
				if (!$GLOBALS['more']) {
					$this->imdb_result = $this->_imdb_fetch_from_net("Find","select=all&for=".$query) or die ("cannot retrieve matching titles");
					if (preg_match("#Location: .*/Title\?(\d{7})#", $this->imdb_result, $result)) {
						$this->imdb_result = $this->_imdb_fetch_from_net("Title", $result[1]);
						$this->IMDBSearch($result[1]);
					}
				} else
					$this->imdb_result   = $this->_imdb_fetch_from_net("Tsearch","".$query) or die ("cannot retrieve matching titles");
				$morer = $this->imdb_get();
				if ($morer)
					$this->imdb_result   = $morer;
			}
		}
	}
	
	function imdb_get_id() { 
		if (preg_match(RE_IMDB_SINGLE, $this->imdb_result, $result))
			return $result[1]; 
	} 
	
	function _get_image_type($url) {
		$str_check = substr($url,0,4);
		if ($str_check == 'http' or $str_check == IMDB_IMG_CHECK) {
			$base = "image/";
			$ext = explode('.',$url);
			$ext = $ext[sizeof($ext)-1];
			if($ext == "jpg")
				return $base."jpeg";
			else
				return $base.$ext;
		} else
			return NULL;
	}
	
	function _get_imdb_img($url) {
		$str_check = substr($url,0,4);
		if ($str_check == 'http' or $str_check == IMDB_IMG_CHECK) {
			ob_start();
			readfile($url);
			$img = ob_get_contents();
			ob_end_clean();
			$size = strlen($img);
			$data = addslashes(fread(fopen($url,"r"), $size));
			return $data;
		} else
			return NULL;
	}
	
	function imdb_get_cover() {
		if (preg_match_all(RE_IMDB_COVER,$this->imdb_result, $result))
			$res = $result[1][0];
		else
			$res = null;
		return $res;
	}
	
	function imdb_get_rating() {
		if (preg_match_all(RE_IMDB_RATING,$this->imdb_result, $result))
			$rate = $result[1][0];
		else
			$rate = 0;
		return $rate;
	}
	
	function imdb_get_genre_arr() {
		if (preg_match(RE_IMDB_GENRE, $this->imdb_result, $result))
			$res = explode(" / ",strip_tags($result[1]));
		else
			$res = array("Onbekend");
		return $res;
	}
	
	function imdb_get_directors_arr(){
		$director = substr($this->imdb_credits,strpos($this->imdb_credits,"Directed by")+29);
		$director = substr($director,1,strpos($director,"<tr><td colspan=\"4\">")-1);
		$director = explode("</tr><tr>",$director);
		if (is_array($director)) {
			foreach ($director as $dir) {
				if (preg_match_all(RE_IMDB_DIRECTOR, $dir, $result, PREG_PATTERN_ORDER))
					$res[]["name"] = $result[2][0];
			}
			return $res;
		} else
			return null;
	} 
	
	function imdb_get_cast_arr(){
		$cast = substr($this->imdb_credits,strpos($this->imdb_credits,"<b class=\"blackcatheader\">Cast</b>"));
		$cast = substr($cast,1,strpos($cast,"</table>")); 
		if (preg_match_all(RE_IMDB_CAST, $cast, $result, PREG_PATTERN_ORDER)) { 
			$ret = "";
			if (is_array($result[0])) {
				$i = 0;
				foreach ($result[0] as $rec) {
					$val = explode(" .... ",$rec);
					$ret[$i]["Name"] = strip_tags($val[0]);
					$ret[$i]["Character"] = strip_tags($val[1]);
					$i++;
				}
			}
			$res = $ret;
	
	
		} else
			$res = null; 
		return $res; 
	} 
	
	function imdb_get_tagline(){
		if (preg_match(RE_IMDB_TAGLINE, $this->imdb_result, $result)) {
			$res = strip_tags($result[1]);
			$res = str_replace('(more)', '', $res);
			$res = trim($res);
		} else
			$res = null;
		return $res;
	}
	
	function imdb_get_year($search){
		if (preg_match_all(RE_IMDB_YEAR,$search, $result))
			$res = $result[1][0];
		else
			$res = null;
		return $res;
	}
	
	function imdb_get_title(){
		$pos = strpos($this->imdb_result,"<title>") + 7;
		$raw = substr($this->imdb_result,$pos,strpos($this->imdb_result,"</title>") - $pos);
		$res = substr($raw,0,strpos($raw," ("));
		return $res;
	}
	
	function imdb_get_plot_outline(){
		if (preg_match(RE_IMDB_OUTLINE, $this->imdb_result, $result))
			$res = strip_tags($result[1]);
		else
			$res = null;
		return $res;
	}
	
	function imdb_get_plot_outline_more(){
		if (preg_match(RE_IMDB_OUTLINE_MORE, $this->imdb_result, $result)) {
			$res = strip_tags($result[1]);
			$res = $this->_imdb_fetch_from_net("Plot",$res) or die ("cannot retrieve extended outline");
			$res = substr($res,strpos($res,'<p class="plotpar">')+19,strpos($res,'</p>') - 1 - strpos($res,'<p class="plotpar">')-18);
		} else
			$res = null;
		return $res;
	}
	
	function imdb_get() {
		if (preg_match_all(RE_IMDB_MULTI,$this->imdb_result, $result)) {
			$ret = "";
			foreach ($result[1] as $key=>$val) {
				$ret["multiple"][$val]["Id"] = $val;
				$ret["multiple"][$val]["Title"] = $result[2][$key];
			}
		} else if (preg_match_all(RE_IMDB_SINGLE,$this->imdb_result, $result))
			$ret = FALSE;
		else
			$ret = FALSE;
		return $ret;
	}

}
?>
Return current item: IMDBSearch