Location: PHPKode > scripts > IMDb Fetch > imdb-fetch/IMDbFetch.class.php
<?php
/**********************************\
||================================||
||                                ||
||        IMDbFetch v0.1          ||
||     IMDbFetch.class.php        ||
||              |                 ||
||          - class -             ||
||   by teo rusu                  ||
||   hide@address.com          ||
||   http://- :) not yet, no time ||
||                                ||
||================================||
\\************************/////////
                         /////////
                        /////////
//
ini_set("max_execution_time","20000");
ini_set("max_input_time","20000");
//
//require the mighty snoopy class :)
//
require_once("snoopy.class.php");
//
//define some needed functions:
//
function cleanString($imputString) {
	$whatToCleanArray = array(chr(13),chr(10),chr(13).chr(10),chr(10).chr(13),"\n","  ","   ","    ","\n\n","\n\r");
	$cleanWithArray = array("","","","","","","","","","");
	$cleaned = str_replace($whatToCleanArray,$cleanWithArray,$imputString);
	$cleaned = trim($cleaned);
	return $cleaned;
}
//
function getExt($filename) {
	return substr(strrchr($filename,"."),1);
}
//
//define imdb fetch class:
//
class IMDbFetch {
	//
	var $addressLink;
	//
	var $id;
	var $name;
	var $year;
	var $directed_by;
	var $writing_credits;
	var $genre;
	var $rating;
	var $runtime;
	var $comment;
	var $poster_image;
	var $poster_image_link;
	var $poster_image_name;
	//for internal use:
	var $instance_id;
	//
	function IMDbFetch($imputLink) {
		if(empty($imputLink)) {
			$this->addressLink = "http://www.imdb.com/title/tt0259711/";
		}
		else {
			$this->addressLink = $imputLink;
		}
		$snoopy = new Snoopy();
		$snoopy->fetchtext($this->addressLink);
		$linkContens = $snoopy->results;
		//
		//generate a prefix for the temp files(just in case ;) ):
		$tempFilePrefix = rand(1001,9001);
		$this->instance_id = $tempFilePrefix;
		//
		$fp1 = fopen($this->instance_id.'_'."tempimdbfetch","wt");
		$fp2 = fwrite($fp1,$linkContens);
		$fp3 = fclose($fp1);
		//
		$searchPage = file_get_contents($this->instance_id.'_'."tempimdbfetch");
		//
		//IMDB NAME:
		//
		$imdbNameStartTag    = 'submit a comment';
		$imdbNameStartTagPos = strpos($searchPage,$imdbNameStartTag);
		$imdbNameEndTag      = ' (';
		$imdbNameEndTagPos   = strpos($searchPage,$imdbNameEndTag,$imdbNameStartTagPos);
		$bruteImdbName = substr($searchPage,$imdbNameStartTagPos+strlen($imdbNameStartTag),$imdbNameEndTagPos-$imdbNameStartTagPos-strlen($imdbNameStartTag));
		$cleanImdbName = str_replace(chr(13).chr(10),"",str_replace(chr(10).chr(13),"",$bruteImdbName));
		$cleanImdbName = str_replace("    "," ",$cleanImdbName);
		$cleanImdbName = str_replace("   ","",$cleanImdbName);
		$imdbName = cleanString($cleanImdbName);
		$this->name = $imdbName;
		//
		//IMDB YEAR:
		//
		$imdbYearStartTag    = 'submit a comment'.chr(13);
		$imdbYearStartTagPos = strpos($searchPage,$imdbYearStartTag);
		$imdbYearEndTag      = ')'.chr(13);
		$imdbYearEndTagPos   = strpos($searchPage,$imdbYearEndTag,$imdbYearStartTagPos);
		$imdbYearArray = explode("(", substr($searchPage,$imdbYearStartTagPos+strlen($imdbYearStartTag),$imdbYearEndTagPos-$imdbYearStartTagPos-strlen($imdbYearStartTag)));
		$imdbYear = cleanString($imdbYearArray[1]);
		$this->year = $imdbYear;
		//
		//IMDB DIRECTED:
		//
		$imdbDirectedStartTag    = 'Directed by';
		$imdbDirectedStartTagPos = strpos($searchPage,$imdbDirectedStartTag);
		$imdbDirectedEndTag      = 'Writing credits';
		$imdbDirectedEndTagPos   = strpos($searchPage,$imdbDirectedEndTag,$imdbDirectedStartTagPos);
		$imdbDirected = substr($searchPage,$imdbDirectedStartTagPos+strlen($imdbDirectedStartTag),$imdbDirectedEndTagPos-$imdbDirectedStartTagPos-strlen($imdbDirectedStartTag));
		$this->directed_by = cleanString($imdbDirected);
		//
		//IMDB WRITING CREDITS:
		//
		$imdbWritingCreditsStartTag    = 'Writing credits';
		$imdbWritingCreditsStartTagPos = strpos($searchPage,$imdbWritingCreditsStartTag);
		$imdbWritingCreditsEndTag      = 'Add to MyMovies';
		$imdbWritingCreditsEndTagPos   = strpos($searchPage,$imdbWritingCreditsEndTag,$imdbWritingCreditsStartTagPos);
		$imdbWritingCredits = substr($searchPage,$imdbWritingCreditsStartTagPos+strlen($imdbWritingCreditsStartTag),$imdbWritingCreditsEndTagPos-$imdbWritingCreditsStartTagPos-strlen($imdbWritingCreditsStartTag));
		$this->writing_credits = cleanString($imdbWritingCredits);
		//
		//IMDB GENRE:
		//
		$imdbGenreStartTag    = 'Genre:';
		$imdbGenreStartTagPos = strpos($searchPage,$imdbGenreStartTag);
		$imdbGenreEndTag      = ' (more)';
		$imdbGenreEndTagPos   = strpos($searchPage,$imdbGenreEndTag,$imdbGenreStartTagPos);
		$imdbGenre = substr($searchPage,$imdbGenreStartTagPos+strlen($imdbGenreStartTag),$imdbGenreEndTagPos-$imdbGenreStartTagPos-strlen($imdbGenreStartTag));
		$this->genre = cleanString($imdbGenre);
		//
		//IMDB RATING:
		//
		$imdbRatingStartTag    = 'User Rating: ';
		$imdbRatingStartTagPos = strpos($searchPage,$imdbRatingStartTag);
		$imdbRatingEndTag      = ' votes)';
		$imdbRatingEndTagPos   = strpos($searchPage,$imdbRatingEndTag,$imdbRatingStartTagPos);
		$imdbRatingArray = explode(" (", substr($searchPage,$imdbRatingStartTagPos+strlen($imdbRatingStartTag),$imdbRatingEndTagPos-$imdbRatingStartTagPos-strlen($imdbRatingStartTag)));
		$imdbRating = $imdbRatingArray[0].' ('.$imdbRatingArray[1].' votes)';
		$this->rating = $imdbRating;
		//
		//IMDB RUNTIME:
		//
		$imdbRuntimeStartTag    = 'Runtime:';
		$imdbRuntimeStartTagPos = strpos($searchPage,$imdbRuntimeStartTag);
		$imdbRuntimeEndTag      = 'Country:';
		$imdbRuntimeEndTagPos   = strpos($searchPage,$imdbRuntimeEndTag,$imdbRuntimeStartTagPos);
		$imdbRuntime = substr($searchPage,$imdbRuntimeStartTagPos+strlen($imdbRuntimeStartTag),$imdbRuntimeEndTagPos-$imdbRuntimeStartTagPos-strlen($imdbRuntimeStartTag));
		$this->runtime = cleanString($imdbRuntime);
		//
		//IMDB COMMENT:
		//
		$imdbCommentStartTag    = 'people found the following comment useful:-';
		$imdbCommentStartTagPos = strpos($searchPage,$imdbCommentStartTag);
		$imdbCommentEndTag      = 'Was the above comment useful to you?';
		$imdbCommentEndTagPos   = strpos($searchPage,$imdbCommentEndTag,$imdbCommentStartTagPos);
		$imdbComment = substr($searchPage,$imdbCommentStartTagPos+strlen($imdbCommentStartTag),$imdbCommentEndTagPos-$imdbCommentStartTagPos-strlen($imdbCommentStartTag));
		$this->comment = $imdbComment;
		//
		//IMDB ID:
		//
		$imdbIDStartTag    = 'title/tt';
		$imdbIDStartTagPos = strpos($this->addressLink,$imdbIDStartTag);
		$imdbIDEndTag      = '/';
		$imdbIDEndTagPos   = strpos($this->addressLink,$imdbIDEndTag,$imdbIDStartTagPos);
		$imdbID = substr($this->addressLink,$imdbIDStartTagPos+strlen($imdbIDStartTag),$imdbIDEndTagPos-$imdbIDStartTagPos-strlen($imdbIDStartTag)+2);
		$this->id = cleanString($imdbID);
		//
		//IMDB IMAGE:
		//
		$snoopyImdbImage = new Snoopy();
		$snoopyImdbImage->fetch($this->addressLink);
		$imdbImageString = $snoopyImdbImage->results;
		//
		$imdbImageStartTag    = 'alt="'.$cleanImdbName.'" title="'.$cleanImdbName.'" src="';
		$imdbImageStartTagPos = strpos($imdbImageString,$imdbImageStartTag);
		$imdbImageEndTag      = '" height=';
		$imdbImageEndTagPos   = strpos($imdbImageString,$imdbImageEndTag,$imdbImageStartTagPos);
		$imdbImageLink = substr($imdbImageString,$imdbImageStartTagPos+strlen($imdbImageStartTag),$imdbImageEndTagPos-$imdbImageStartTagPos-strlen($imdbImageStartTag));
		$imdbImageExtension = getExt($imdbImageLink);
		copy($imdbImageLink,$this->instance_id.'_'."tempimdbimage");
		$instr = fopen($this->instance_id.'_'."tempimdbimage","rb");
		$imdbImageData = addslashes(fread($instr,filesize($this->instance_id.'_'."tempimdbimage")));
		$imdbImageArray = array($imdbImageData,$imdbImageExtension);
		fclose($instr);
		$this->poster_image = $imdbImageArray;
		$this->poster_image_link = $imdbImageLink;
		//
		unlink($this->instance_id.'_'."tempimdbfetch");
		unlink($this->instance_id.'_'."tempimdbimage");
		//
		$posterExt = getExt($this->poster_image_link);
		$posterName = $this->instance_id.'_'.$this->id.'.'.$posterExt;
		copy($this->poster_image_link,$posterName);
		$this->poster_image_name = $posterName;
		//
		return true;
	}
	//function to display poster image:
	function displayPoster() {
		echo '<img src="'.$this->poster_image_name.'" name="'.$this->name.'" alt="'.$this->name.'">';
	}
}
?>
Return current item: IMDb Fetch