我的一位读者 Fabian Beiner 进一步改进了我基于 PHP 的 IMDB Grabber 脚本,并将其变成了一个有用的类。该课程还能够获取我的脚本所做的更多信息。检查一下!
PHP
<?php
/**
* @author Fabian Beiner <fabianDOTbeinerATgmailDOTcom>
* @version 2.1alpha
*
* @comment Original idea by David Walsh <davidATdavidwalshDOTname>, 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('|<title>(.*) \((.*)\)</title>|Uis', $imdb_content));
$this->director = trim($this->getMatch('|<h5>Director:</h5><a href="(.*)">(.*)</a><br/>|Uis', $imdb_content, 2));
$this->url_director = trim($this->getMatch('|<h5>Director:</h5><a href="(.*)">(.*)</a><br/>|Uis', $imdb_content));
$this->plot = trim($this->getMatch('|<h5>Plot:</h5>(.*) <a|Uis', $imdb_content));
$this->release_date = trim($this->getMatch('|<h5>Release Date:</h5> (.*) \((.*)\) <a|Uis', $imdb_content));
$this->mpaa = trim($this->getMatch('|<h5><a href="/mpaa">MPAA</a>:</h5> (.*)</div>|Uis', $imdb_content));
$this->run_time = trim($this->getMatch('|Runtime:</h5>(.*) (.*)</div>|Uis',$imdb_content));
$this->rating = trim($this->getMatch('|<div ><b>(.*)</b>|Uis', $imdb_content));
$this->votes = trim($this->getMatch('| <a href="ratings" >(.*) votes</a>|Uis', $imdb_content));
$this->country = trim($this->getMatch('|<h5>Country:</h5><a href="(.*)">(.*)</a></div>|Uis', $imdb_content, 2));
$this->url_country = trim($this->getMatch('|<h5>Country:</h5><a href="(.*)">(.*)</a></div>|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('|<p ><b>Media from <a href="(.*)">(.*)</a> ((.*))</b></p>|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.= '<h2>Film</h2><p>'.$this->movie.'</p>';
$content.= '<h2>Director</h2><p><a href="http://www.imdb.com'.$this->url_director.'">'.$this->director.'</a></p>';
$content.= '<h2>Plot</h2><p>'.$this->plot.'</p>';
$content.= '<h2>Release Date</h2><p>'.$this->release_date.'</p>';
$content.= '<h2>MPAA</h2><p>'.$this->mpaa.'</p>';
$content.= '<h2>Run Time</h2><p>'.$this->run_time.' minutes</p>';
$content.= '<h2>Full Details</h2><p><a href="'.$this->url.'">'.$this->url.'</a></p>';
$content.= '<h2>Rating</h2><p>'.$this->rating.'</p>';
$content.= '<h2>Votes</h2><p>'.$this->votes.' votes</p>';
$content.= '<h2>Country</h2><p><a href="http://www.imdb.com'.$this->url_country.'">'.$this->country.'</a></p>';
echo $content;
} else {
echo 'Sorry, nothing found! :(';
}
}
}
// Examples :)
$imdb = new IMDB('http://www.imdb.com/title/tt0367882/');
print($imdb->showOutput());
echo '<hr>';
$imdb = new IMDB('Cruel Intentions');
print($imdb->showOutput());
echo '<hr>';
$imdb = new IMDB('I guess this movie name doesnt exists');
print($imdb->showOutput());
?>
感谢 Fabian 的分享!享受吧!
