【问题标题】:Grab imdb poster image from search term using php使用 php 从搜索词中获取 imdb 海报图片
【发布时间】:2012-05-19 04:53:04
【问题描述】:

我想从搜索词中使用来自 imdb 的 php 获取海报图片 url。例如,我有搜索词 21 Jump Street,我想取回图像 ur 或仅 imdb 电影 url。使用下面的代码,我只需要从搜索词中检索电影的网址

这是我的代码

<?php

    include("simple_html_dom.php");

//url to imdb page
$url = 'hereistheurliwanttogetfromsearch';

//get the page content
$imdb_content = file_get_contents($url);

$html = str_get_html($imdb_content);

$name = $html->find('title',0)->plaintext;

$director = $html->find('a[itemprop="director"]',0)->innertext;

$plot = $html->find('p[itemprop="description"]',0)->innertext;

$release_date = $html->find('time[itemprop="datePublished"]',0)->innertext;

$mpaa = $html->find('span[itemprop="contentRating"]',0)->innertext;

$run_time = $html->find('time[itemprop="duration"]',0)->innertext;

$img = $html->find('img[itemprop="image"]',0)->src;

$content = "";

//build content
$content.= '<h2>Film</h2><p>'.$name.'</p>';
$content.= '<h2>Director</h2><p>'.$director.'</p>';
$content.= '<h2>Plot</h2><p>'.$plot.'</p>';
$content.= '<h2>Release Date</h2><p>'.$release_date.'</p>';
$content.= '<h2>MPAA</h2><p>'.$mpaa.'</p>';
$content.= '<h2>Run Time</h2><p>'.$run_time.'</p>';
$content.= '<h2>Full Details</h2><p><a href="'.$url.'" rel="nofollow">'.$url.'</a></p>';
$content.= '<img src="'.$img.'" />';

echo $content;

?>

【问题讨论】:

  • 到目前为止你有什么?
  • 我将发布一些我在网上找到的代码。我是 php 新手,....我将编辑并添加上面的代码

标签: php imdb


【解决方案1】:

使用 Kasper Mackenhauer Jacobsenless 建议的 API 可以得到更完整的答案:

$url = 'http://www.imdbapi.com/?i=&t=21+jump+street';

$json_response = file_get_contents($url);
$object_response = json_decode($json_response);

if(!is_null($object_response) && isset($object_response->Poster)) {
        $poster_url = $object_response->Poster;
        echo $poster_url."\n";
}

【讨论】:

    【解决方案2】:

    使用正则表达式进行解析很糟糕,但其中几乎没有什么可以破坏的。建议使用 curl 更快,您可以屏蔽您的用户代理。

    从搜索中获取图像的主要问题是您首先需要知道 IMDB ID,然后才能加载页面并翻录图像 url。希望对你有帮助

    <?php 
    //Is form posted
    if($_SERVER['REQUEST_METHOD']=='POST'){
        $find = $_POST['find'];
    
        //Get Imdb code from search
        $source = file_get_curl('http://www.imdb.com/find?q='.urlencode(strtolower($find)).'&s=tt');
        if(preg_match('#/title/(.*?)/mediaindex#',$source,$match)){
            //Get main page for imdb id
            $source = file_get_curl('http://www.imdb.com/title/'.$match[1]);
            //Grab the first .jpg image, which is always the main poster
            if(preg_match('#<img src=\"(.*).jpg\"#',$source,$match)){
                $imdb=$match[1];
                //do somthing with image
                echo '<img src="'.$imdb.'" />';
            }
        }
    }
    
    //The curl function
    function file_get_curl($url){
    (function_exists('curl_init')) ? '' : die('cURL Must be installed');
    $curl = curl_init();
    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: ";
    
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_REFERER, $url);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    
    $html = curl_exec($curl);
    
    $status = curl_getinfo($curl);
    curl_close($curl);
    
    if($status['http_code'] != 200){
        if($status['http_code'] == 301 || $status['http_code'] == 302) {
            list($header) = explode("\r\n\r\n", $html, 2);
            $matches = array();
            preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
            $url = trim(str_replace($matches[1],"",$matches[0]));
            $url_parsed = parse_url($url);
            return (isset($url_parsed))? file_get_curl($url):'';
        }
        return FALSE;
    }else{
        return $html;
    }
    }
    ?>
    <form method="POST" action="">
    <p><input type="text" name="find" size="20"><input type="submit" value="Submit"></p>
    </form> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 2011-06-18
      • 2011-09-26
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多