php
  • imdb
  • 2012-05-10 37 views 0 likes 
    0

    我想從搜索字詞中使用來自imdb的php獲取海報圖像url。例如,我有搜索詞21跳街,我想回到你的圖像或只有IMDB電影網址。隨着下面的代碼,我只需要在搜索詞從搜索字詞使用php抓取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; 
    
    ?> 
    
    +1

    你有什麼這麼遠嗎? – jeroen

    +0

    我會發布一些我在網上找到的代碼。我是新手在PHP中,....我將編輯並添加上面的代碼 – user1346598

    回答

    2

    使用卡斯帕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"; 
    } 
    
    +0

    這是真棒... –

    0

    用正則表達式是不好的解析,但在這一點,可以打破很少有。它建議使用捲曲,因爲它更快,你可以掩蓋你的使用。

    從搜索中獲取圖像的主要問題是您首先需要知道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> 
    
    相關問題