Search code examples
phpimdb

Grab imdb poster image from search term using php


i would like to get the poster image url using php from imdb from a search term. For example i have the search term 21 Jump Street and i would like to get back the image ur or only the imdb movie url. With the below code i need only to retrieve the url of the movie from a search term

here is the code i have

<?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;

?>

Solution

  • Using the API that Kasper Mackenhauer Jacobsenless suggested here's a fuller answer:

    $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";
    }