One of my readers, Fabian Beiner, took my script a step further and made it into a useful class. The class also has the ability to grab more information my script did. Check it out!
我的一位读者Fabian Beiner将脚本更进一步,并将其变成了有用的类。 该类还可以获取脚本执行的更多信息。 看看这个!
PHP (The PHP)
* @version 2.1alpha * * @comment Original idea by David Walsh, thanks! Your blog rocks ;) * I did this script in the middle of the night while being ill, no guarantee for anything! * * @license http://creativecommons.org/licenses/by-sa/3.0/de/deed.en_US * Creative Commons Attribution-Share Alike 3.0 Germany * * Yay, after two days IMDB changed their layout... Great! :( Also added a fallback if cURL is missing. */class IMDB { function __construct($url) { $this->gotCurl = extension_loaded('curl'); $imdb_content = $this->imdbHandler($url); $this->movie = trim($this->getMatch('| (.*) \((.*)\) |Uis', $imdb_content)); $this->director = trim($this->getMatch('|Director:
(.*) |Uis', $imdb_content, 2)); $this->url_director = trim($this->getMatch('|Director:
(.*) |Uis', $imdb_content)); $this->plot = trim($this->getMatch('|Plot:
(.*) release_date = trim($this->getMatch('|Release Date:
(.*) \((.*)\) mpaa = trim($this->getMatch('|MPAA:
(.*)|Uis', $imdb_content)); $this->run_time = trim($this->getMatch('|Runtime:(.*) (.*)|Uis',$imdb_content)); $this->rating = trim($this->getMatch('| |Uis', $imdb_content, 2)); $this->url_country = trim($this->getMatch('|Country:
(.*)|Uis', $imdb_content)); } function imdbHandler($input) { if (!$this->getMatch('|^http://(.*)$|Uis', $input)) { $tmpUrl = 'http://us.imdb.com/find?s=all&q='.str_replace(' ', '+', $input).'&x=0&y=0'; if ($this->gotCurl) { $ch = curl_init(); curl_setopt_array($ch, array(CURLOPT_URL => $tmpUrl, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10 ) ); $data = curl_exec($ch); curl_close($ch); } else { $data = file_get_contents($tmpUrl); } $foundMatch = $this->getMatch('|Media from (.*) ((.*))
|Uis', $data); if ($foundMatch) { $this->url = 'http://www.imdb.com'.$foundMatch; } else { $this->url = ''; return 0; } } else { $this->url = $input; } if ($this->gotCurl) { $ch = curl_init(); curl_setopt_array($ch, array(CURLOPT_URL => $this->url, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10 ) ); $data = curl_exec($ch); curl_close($ch); } else { $data = file_get_contents($this->url); } return str_replace("\n",'',(string)$data); } function getMatch($regex, $content, $index=1) { preg_match($regex, $content, $matches); return $matches[(int)$index]; } function showOutput() { if ($this->url) { $content.= 'Film
'.$this->movie.'
'; $content.= 'Director
'; $content.= 'Plot
'.$this->plot.'
'; $content.= 'Release Date
'.$this->release_date.'
'; $content.= 'MPAA
'.$this->mpaa.'
'; $content.= 'Run Time
'.$this->run_time.' minutes
'; $content.= 'Full Details
'; $content.= 'Rating
'.$this->rating.'
'; $content.= 'Votes
'.$this->votes.' votes
'; $content.= 'Country
'; echo $content; } else { echo 'Sorry, nothing found! :('; } }}// Examples :)$imdb = new IMDB('http://www.imdb.com/title/tt0367882/');print($imdb->showOutput());echo '
';$imdb = new IMDB('Cruel Intentions');print($imdb->showOutput());echo '
';$imdb = new IMDB('I guess this movie name doesnt exists');print($imdb->showOutput());?>
Thanks to Fabian for sharing! Enjoy!
感谢Fabian的分享! 请享用!
翻译自: