博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fabian Beiner撰写的IMDB Grabber类
阅读量:2519 次
发布时间:2019-05-11

本文共 4974 字,大约阅读时间需要 16 分钟。

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)); $this->votes = trim($this->getMatch('|  
(.*) votes|Uis', $imdb_content)); $this->country = trim($this->getMatch('|
Country:
(.*)
|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

'.$this->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

'.$this->url.'

'; $content.= '

Rating

'.$this->rating.'

'; $content.= '

Votes

'.$this->votes.' votes

'; $content.= '

Country

'.$this->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的分享! 请享用!

翻译自:

转载地址:http://yvpwd.baihongyu.com/

你可能感兴趣的文章
一篇文章掌握nightwatch自动化测试
查看>>
vue 上传图片到阿里云(前端直传:不推荐)
查看>>
求最大区间,使得特定的两个数的个数相等
查看>>
node读写Excel操作
查看>>
双飞翼布局
查看>>
android利用wireshark抓包
查看>>
Mac和Windows系统下Mysql数据库的导入导出
查看>>
第五周学习进度情况
查看>>
原生js怎么判断一个对象是否为空
查看>>
CRM客户关系管理系统(八)
查看>>
洛谷P3195 [HNOI2008]玩具装箱TOY 斜率优化
查看>>
字节输入流 FileInputStream
查看>>
swt combo 自动补全
查看>>
Dynamically Creating Bound and Template Columns in GridView
查看>>
当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值(转)
查看>>
easyui datagrid使用参考
查看>>
Linux系统快速启动方案
查看>>
Redis持久化方式
查看>>
选择器+盒模型
查看>>
进度条2
查看>>