diff --git a/imdb.class.php b/imdb.class.php index 10d1171..34c491b 100644 --- a/imdb.class.php +++ b/imdb.class.php @@ -1,4 +1,5 @@ * @license http://opensource.org/licenses/MIT The MIT License * @link https://github.com/FabianBeiner/PHP-IMDB-Grabber GitHub Repository - * @version 6.0.0 + * @version 6.0.1 */ - -class IMDB { +class IMDB +{ /** * Set this to true if you run into problems. */ const IMDB_DEBUG = false; /** - * Set the prefered language for the User Agent. + * Set the preferred language for the User Agent. */ const IMDB_LANG = 'en, en-US;q=0.8'; @@ -97,7 +98,7 @@ class IMDB { const IMDB_COUNTRY = '~(.*)~Ui'; const IMDB_CREATOR = '~
(?:Creator|Creators):<\/h5>(?:\s*)
(.*)<\/div>~Ui'; const IMDB_DIRECTOR = '~
(?:Director|Directors):<\/h5>(?:\s*)
(.*)<\/div>~Ui'; - const IMDB_GENRE = '~(.*)<\/a>~Ui'; + const IMDB_GENRE = '~(.*)<\/a>~Ui'; const IMDB_ID = '~((?:tt\d{6,})|(?:itle\?\d{6,}))~'; const IMDB_LANGUAGE = '~(.*)<\/a>~Ui'; const IMDB_LOCATION = '~href="\/search\/title\?locations=(.*)">(.*)<\/a>~Ui'; @@ -125,11 +126,10 @@ class IMDB { const IMDB_YEAR = '~(.*)<\/a>~Ui'; - - /** - * @param string $sSearch IMDb URL or movie title to search for. - * @param null $iCache Custom cache time in minutes. + * @param string $sSearch IMDb URL or movie title to search for. + * @param null $iCache Custom cache time in minutes. + * @param string $sSearchFor What to search for? * * @throws \IMDBException */ @@ -144,13 +144,11 @@ public function __construct($sSearch, $iCache = null, $sSearchFor = 'all') { if (!function_exists('curl_init')) { throw new IMDBException('You need to enable the PHP cURL extension.'); } - if (in_array($sSearchFor, array( - 'movie', - 'tv', - 'episode', - 'game', - 'all' - ))) { + if (in_array($sSearchFor, array('movie', + 'tv', + 'episode', + 'game', + 'all'))) { $this->sSearchFor = $sSearchFor; } if (true === self::IMDB_DEBUG) { @@ -267,11 +265,9 @@ private function fetchUrl($sSearch) { return false; } - $this->sSource = str_replace(array( - "\n", - "\r\n", - "\r" - ), '', $sSource); + $this->sSource = str_replace(array("\n", + "\r\n", + "\r"), '', $sSource); $this->isReady = true; // Save cache. @@ -306,36 +302,27 @@ public function getAka() { * @return string The aka name. */ public function getAkas() { - - if (true === $this->isReady) { - // Does a cache of this movie exist? + // Does a cache of this movie exist? $sCacheFile = $this->sRoot . '/cache/' . md5($this->iId) . '_akas.cache'; - $bUseCache = false; - + $bUseCache = false; if (is_readable($sCacheFile)) { - $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60); if ($iDiff < $this->iCache || false) { - if (true === self::IMDB_DEBUG) { - echo '
Using cache: ' . basename($sCacheFile) . '
'; - } $bUseCache = true; - $sSource = file_get_contents($sCacheFile); } } if ($bUseCache) { - if (IMDB::IMDB_DEBUG) { - echo '- Using cache for Akas from ' . $sCacheFile . '
'; - } $aRawReturn = file_get_contents($sCacheFile); - $aReturn = unserialize($aRawReturn); + $aReturn = unserialize($aRawReturn); + return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, $this->sNotFound, $aReturn); - } else { - $fullAkas = sprintf('http://www.imdb.com/title/tt%s/releaseinfo', $this->iId); + } + else { + $fullAkas = sprintf('http://www.imdb.com/title/tt%s/releaseinfo', $this->iId); $aCurlInfo = IMDBHelper::runCurl($fullAkas); $sSource = $aCurlInfo['contents']; @@ -352,12 +339,14 @@ public function getAkas() { if ($aReturned) { $aReturn = array(); foreach ($aReturned[1] as $i => $strName) { - if (strpos($strName,'(')===false){ - $aReturn[] = array('title'=>IMDBHelper::cleanString($aReturned[2][$i]), 'country'=> IMDBHelper::cleanString($strName)); + if (strpos($strName, '(') === false) { + $aReturn[] = array('title' => IMDBHelper::cleanString($aReturned[2][$i]), + 'country' => IMDBHelper::cleanString($strName)); } } file_put_contents($sCacheFile, serialize($aReturn)); + return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, $this->sNotFound, $aReturn); } } @@ -415,6 +404,7 @@ public function getCast($iLimit = 0, $bMore = true) { $bMore = (0 !== $iLimit && $bMore && (count($aMatch[2]) > $iLimit) ? '…' : ''); $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit)); + return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, $this->sNotFound, $aReturn, $bHaveMore); } } @@ -441,6 +431,7 @@ public function getCastAsUrl($iLimit = 0, $bMore = true, $sTarget = '') { } $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit)); + return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, $this->sNotFound, $aReturn, $bHaveMore); } } @@ -468,6 +459,7 @@ public function getCastAndCharacter($iLimit = 0, $bMore = true) { } $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit)); + return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, $this->sNotFound, $aReturn, $bHaveMore); } @@ -498,6 +490,7 @@ public function getCastAndCharacterAsUrl($iLimit = 0, $bMore = true, $sTarget = } $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit)); + return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, $this->sNotFound, $aReturn, $bHaveMore); } @@ -815,6 +808,8 @@ public function getPlotKeywords() { } /** + * @param int $iLimit The limit. + * * @return string The plot of the movie or $sNotFound. */ public function getPlot($iLimit = 0) { @@ -893,16 +888,17 @@ public function getReleaseDate() { /** - * Release date doesn't contain all the information we need to create a media and - * we need this function that checks if users can vote target media (if can, it's released). - * - * @return true If the media is released - */ + * Release date doesn't contain all the information we need to create a media and + * we need this function that checks if users can vote target media (if can, it's released). + * + * @return true If the media is released + */ public function isReleased() { $strReturn = $this->getReleaseDate(); if ($strReturn == $this->sNotFound || $strReturn == 'Not yet released') { return false; } + return true; } @@ -1118,186 +1114,96 @@ public function getYear() { */ public function getAll() { $aData = array(); - $aData['Aka'] = array( - 'name' => 'Also Known As', - 'value' => $this->getAka() - ); - $aData['Akas'] = array( - 'name' => '(all) Also Known As', - 'value' => $this->getAkas() - ); - $aData['AspectRatio'] = array( - 'name' => 'Aspect Ratio', - 'value' => $this->getAspectRatio() - ); - $aData['Awards'] = array( - 'name' => 'Awards', - 'value' => $this->getAwards() - ); - $aData['CastLinked'] = array( - 'name' => 'Cast', - 'value' => $this->getCastAsUrl() - ); - $aData['Cast'] = array( - 'name' => 'Cast', - 'value' => $this->getCast() - ); - $aData['CastAndCharacterLinked'] = array( - 'name' => 'Cast and Character', - 'value' => $this->getCastAndCharacterAsUrl() - ); - $aData['CastAndCharacter'] = array( - 'name' => 'Cast and Character', - 'value' => $this->getCastAndCharacter() - ); - $aData['Certification'] = array( - 'name' => 'Certification', - 'value' => $this->getCertification() - ); - $aData['Color'] = array( - 'name' => 'Color', - 'value' => $this->getColor() - ); - $aData['CompanyLinked'] = array( - 'name' => 'Company', - 'value' => $this->getCompanyAsUrl() - ); - $aData['Company'] = array( - 'name' => 'Company', - 'value' => $this->getCompany() - ); - $aData['CountryLinked'] = array( - 'name' => 'Country', - 'value' => $this->getCountryAsUrl() - ); - $aData['Country'] = array( - 'name' => 'Country', - 'value' => $this->getCountry() - ); - $aData['CreatorLinked'] = array( - 'name' => 'Creator', - 'value' => $this->getCreatorAsUrl() - ); - $aData['Creator'] = array( - 'name' => 'Creator', - 'value' => $this->getCreator() - ); - $aData['DirectorLinked'] = array( - 'name' => 'Director', - 'value' => $this->getDirectorAsUrl() - ); - $aData['Director'] = array( - 'name' => 'Director', - 'value' => $this->getDirector() - ); - $aData['GenreLinked'] = array( - 'name' => 'Genre', - 'value' => $this->getGenreAsUrl() - ); - $aData['Genre'] = array( - 'name' => 'Genre', - 'value' => $this->getGenre() - ); - $aData['LanguageLinked'] = array( - 'name' => 'Language', - 'value' => $this->getLanguageAsUrl() - ); - $aData['Language'] = array( - 'name' => 'Language', - 'value' => $this->getLanguage() - ); - $aData['LocationLinked'] = array( - 'name' => 'Location', - 'value' => $this->getLocationAsUrl() - ); - $aData['Location'] = array( - 'name' => 'Location', - 'value' => $this->getLocation() - ); - $aData['MovieMeter'] = array( - 'name' => 'MOVIEmeter', - 'value' => $this->getMovieMeter() - ); - $aData['MPAA'] = array( - 'name' => 'MPAA', - 'value' => $this->getMpaa() - ); - $aData['PlotKeywords'] = array( - 'name' => 'Plot Keywords', - 'value' => $this->getPlotKeywords() - ); - $aData['Plot'] = array( - 'name' => 'Plot', - 'value' => $this->getPlot() - ); - $aData['Poster'] = array( - 'name' => 'Poster', - 'value' => $this->getPoster('big') - ); - $aData['Rating'] = array( - 'name' => 'Rating', - 'value' => $this->getRating() - ); - $aData['ReleaseDate'] = array( - 'name' => 'Release Date', - 'value' => $this->getReleaseDate() - ); - $aData['IsReleased'] = array( - 'name' => 'Is released', - 'value' => $this->isReleased() - ); - $aData['Runtime'] = array( - 'name' => 'Runtime', - 'value' => $this->getRuntime() - ); - $aData['SeasonsLinked'] = array( - 'name' => 'Seasons', - 'value' => $this->getSeasonsAsUrl() - ); - $aData['Seasons'] = array( - 'name' => 'Seasons', - 'value' => $this->getSeasons() - ); - $aData['SoundMix'] = array( - 'name' => 'Sound Mix', - 'value' => $this->getSoundMix() - ); - $aData['Tagline'] = array( - 'name' => 'Tagline', - 'value' => $this->getTagline() - ); - $aData['Title'] = array( - 'name' => 'Title', - 'value' => $this->getTitle() - ); - $aData['TrailerLinked'] = array( - 'name' => 'Trailer', - 'value' => $this->getTrailerAsUrl() - ); - $aData['Url'] = array( - 'name' => 'Url', - 'value' => $this->getUrl() - ); - $aData['UserReview'] = array( - 'name' => 'User Review', - 'value' => $this->getUserReview() - ); - $aData['Votes'] = array( - 'name' => 'Votes', - 'value' => $this->getVotes() - ); - $aData['WriterLinked'] = array( - 'name' => 'Writer', - 'value' => $this->getWriterAsUrl() - ); - $aData['Writer'] = array( - 'name' => 'Writer', - 'value' => $this->getWriter() - ); - $aData['Year'] = array( - 'name' => 'Year', - 'value' => $this->getYear() - ); + $aData['Aka'] = array('name' => 'Also Known As', + 'value' => $this->getAka()); + $aData['Akas'] = array('name' => '(all) Also Known As', + 'value' => $this->getAkas()); + $aData['AspectRatio'] = array('name' => 'Aspect Ratio', + 'value' => $this->getAspectRatio()); + $aData['Awards'] = array('name' => 'Awards', + 'value' => $this->getAwards()); + $aData['CastLinked'] = array('name' => 'Cast', + 'value' => $this->getCastAsUrl()); + $aData['Cast'] = array('name' => 'Cast', + 'value' => $this->getCast()); + $aData['CastAndCharacterLinked'] = array('name' => 'Cast and Character', + 'value' => $this->getCastAndCharacterAsUrl()); + $aData['CastAndCharacter'] = array('name' => 'Cast and Character', + 'value' => $this->getCastAndCharacter()); + $aData['Certification'] = array('name' => 'Certification', + 'value' => $this->getCertification()); + $aData['Color'] = array('name' => 'Color', + 'value' => $this->getColor()); + $aData['CompanyLinked'] = array('name' => 'Company', + 'value' => $this->getCompanyAsUrl()); + $aData['Company'] = array('name' => 'Company', + 'value' => $this->getCompany()); + $aData['CountryLinked'] = array('name' => 'Country', + 'value' => $this->getCountryAsUrl()); + $aData['Country'] = array('name' => 'Country', + 'value' => $this->getCountry()); + $aData['CreatorLinked'] = array('name' => 'Creator', + 'value' => $this->getCreatorAsUrl()); + $aData['Creator'] = array('name' => 'Creator', + 'value' => $this->getCreator()); + $aData['DirectorLinked'] = array('name' => 'Director', + 'value' => $this->getDirectorAsUrl()); + $aData['Director'] = array('name' => 'Director', + 'value' => $this->getDirector()); + $aData['GenreLinked'] = array('name' => 'Genre', + 'value' => $this->getGenreAsUrl()); + $aData['Genre'] = array('name' => 'Genre', + 'value' => $this->getGenre()); + $aData['LanguageLinked'] = array('name' => 'Language', + 'value' => $this->getLanguageAsUrl()); + $aData['Language'] = array('name' => 'Language', + 'value' => $this->getLanguage()); + $aData['LocationLinked'] = array('name' => 'Location', + 'value' => $this->getLocationAsUrl()); + $aData['Location'] = array('name' => 'Location', + 'value' => $this->getLocation()); + $aData['MovieMeter'] = array('name' => 'MOVIEmeter', + 'value' => $this->getMovieMeter()); + $aData['MPAA'] = array('name' => 'MPAA', + 'value' => $this->getMpaa()); + $aData['PlotKeywords'] = array('name' => 'Plot Keywords', + 'value' => $this->getPlotKeywords()); + $aData['Plot'] = array('name' => 'Plot', + 'value' => $this->getPlot()); + $aData['Poster'] = array('name' => 'Poster', + 'value' => $this->getPoster('big')); + $aData['Rating'] = array('name' => 'Rating', + 'value' => $this->getRating()); + $aData['ReleaseDate'] = array('name' => 'Release Date', + 'value' => $this->getReleaseDate()); + $aData['IsReleased'] = array('name' => 'Is released', + 'value' => $this->isReleased()); + $aData['Runtime'] = array('name' => 'Runtime', + 'value' => $this->getRuntime()); + $aData['SeasonsLinked'] = array('name' => 'Seasons', + 'value' => $this->getSeasonsAsUrl()); + $aData['Seasons'] = array('name' => 'Seasons', + 'value' => $this->getSeasons()); + $aData['SoundMix'] = array('name' => 'Sound Mix', + 'value' => $this->getSoundMix()); + $aData['Tagline'] = array('name' => 'Tagline', + 'value' => $this->getTagline()); + $aData['Title'] = array('name' => 'Title', + 'value' => $this->getTitle()); + $aData['TrailerLinked'] = array('name' => 'Trailer', + 'value' => $this->getTrailerAsUrl()); + $aData['Url'] = array('name' => 'Url', + 'value' => $this->getUrl()); + $aData['UserReview'] = array('name' => 'User Review', + 'value' => $this->getUserReview()); + $aData['Votes'] = array('name' => 'Votes', + 'value' => $this->getVotes()); + $aData['WriterLinked'] = array('name' => 'Writer', + 'value' => $this->getWriterAsUrl()); + $aData['Writer'] = array('name' => 'Writer', + 'value' => $this->getWriter()); + $aData['Year'] = array('name' => 'Year', + 'value' => $this->getYear()); array_multisort($aData); @@ -1305,7 +1211,8 @@ public function getAll() { } } -class IMDBHelper extends IMDB { +class IMDBHelper extends IMDB +{ /** * Regular expression helper. * @@ -1336,39 +1243,39 @@ public static function matchRegex($sContent, $sPattern, $iIndex = null) { /** * Prefered output in responses with multiple elements * - * @param bool $bArrayOutput Native array or string wtih separators. - * @param string $sSeparator String separator. - * @param string $sNotFound Not found text. - * @param array $aReturn Original input. - * @param bool $bHaveMore Have more elements indicator. + * @param bool $bArrayOutput Native array or string wtih separators. + * @param string $sSeparator String separator. + * @param string $sNotFound Not found text. + * @param array $aReturn Original input. + * @param bool $bHaveMore Have more elements indicator. * * @return string Multiple results separeted by selected separator string. - * @return array Multiple results enclosed into native array. + * @return array Multiple results enclosed into native array. */ public static function arrayOutput($bArrayOutput, $sSeparator, $sNotFound, $aReturn = null, $bHaveMore = false) { - if ($bArrayOutput){ - if ($aReturn == null || !is_array($aReturn)) { - return array(); - } + if ($bArrayOutput) { + if ($aReturn == null || !is_array($aReturn)) { + return array(); + } - if ($bHaveMore) { - $aReturn[] = '…'; - } + if ($bHaveMore) { + $aReturn[] = '…'; + } - return $aReturn; + return $aReturn; } else { - if ($aReturn == null || !is_array($aReturn)) { - return $sNotFound; - } + if ($aReturn == null || !is_array($aReturn)) { + return $sNotFound; + } - foreach ($aReturn as $i => $value) { - if (is_array($value)) { - $aReturn[$i] = implode($sSeparator, $value); + foreach ($aReturn as $i => $value) { + if (is_array($value)) { + $aReturn[$i] = implode($sSeparator, $value); + } } - } - - return implode($sSeparator, $aReturn) . (($bHaveMore) ? '…' : ''); + + return implode($sSeparator, $aReturn) . (($bHaveMore) ? '…' : ''); } } @@ -1380,22 +1287,18 @@ public static function arrayOutput($bArrayOutput, $sSeparator, $sNotFound, $aRet * @return string Cleaned string. */ public static function cleanString($sInput) { - $aSearch = array( - 'Full summary »', - 'Full synopsis »', - 'Add summary »', - 'Add synopsis »', - 'See more »', - 'See why on IMDbPro.' - ); - $aReplace = array( - '', - '', - '', - '', - '', - '' - ); + $aSearch = array('Full summary »', + 'Full synopsis »', + 'Add summary »', + 'Add synopsis »', + 'See more »', + 'See why on IMDbPro.'); + $aReplace = array('', + '', + '', + '', + '', + ''); $sInput = strip_tags($sInput); $sInput = str_replace(' ', ' ', $sInput); $sInput = str_replace($aSearch, $aReplace, $sInput); @@ -1435,23 +1338,19 @@ public static function getShortText($sText, $iLength = 100) { */ public static function runCurl($sUrl, $bDownload = false) { $oCurl = curl_init($sUrl); - curl_setopt_array($oCurl, array( - CURLOPT_BINARYTRANSFER => ($bDownload ? true : false), - CURLOPT_CONNECTTIMEOUT => self::IMDB_TIMEOUT, - CURLOPT_ENCODING => '', - CURLOPT_FOLLOWLOCATION => false, - CURLOPT_FRESH_CONNECT => true, - CURLOPT_HEADER => ($bDownload ? false : true), - CURLOPT_HTTPHEADER => array( - 'Accept-Language:' . self::IMDB_LANG, - 'Accept-Charset:' . 'utf-8, iso-8859-1;q=0.8', - ), - CURLOPT_REFERER => 'http://www.google.com', - CURLOPT_RETURNTRANSFER => true, - CURLOPT_TIMEOUT => self::IMDB_TIMEOUT, - CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', - CURLOPT_VERBOSE => false - )); + curl_setopt_array($oCurl, array(CURLOPT_BINARYTRANSFER => ($bDownload ? true : false), + CURLOPT_CONNECTTIMEOUT => self::IMDB_TIMEOUT, + CURLOPT_ENCODING => '', + CURLOPT_FOLLOWLOCATION => false, + CURLOPT_FRESH_CONNECT => true, + CURLOPT_HEADER => ($bDownload ? false : true), + CURLOPT_HTTPHEADER => array('Accept-Language:' . self::IMDB_LANG, + 'Accept-Charset:' . 'utf-8, iso-8859-1;q=0.8',), + CURLOPT_REFERER => 'http://www.google.com', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => self::IMDB_TIMEOUT, + CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', + CURLOPT_VERBOSE => false)); $sOutput = curl_exec($oCurl); $aCurlInfo = curl_getinfo($oCurl); curl_close($oCurl); @@ -1469,8 +1368,8 @@ public static function runCurl($sUrl, $bDownload = false) { } /** - * @param $sUrl The URL to the image to download. - * @param $iId The ID of the movie. + * @param string $sUrl The URL to the image to download. + * @param int $iId The ID of the movie. * * @return string Local path. */ @@ -1498,5 +1397,6 @@ public static function saveImage($sUrl, $iId) { } } -class IMDBException extends Exception { +class IMDBException extends Exception +{ }