1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Grab Movie Poster from IMDB

Discussion in 'PHP' started by Vick.Kumar, Feb 25, 2013.

  1. #1
    Hi,

    I was going through old files on mediafire, and found a few useful php scripts, so here's a source code to grab the movie image/post from IMDB based on the movie named passed through in the function/param.

    PHP:
    1. <?php
    2. /**
    3. * Grab Movie Poster from IMDB to display on hover
    4. *
    5. * @author Vick Kumar <vickkumar2011@gmail.com>
    6. * @copyright Seecure.me, 2012
    7. * @version 1.0
    8. * @license http://creativecommons.org/licenses/by/3.0/legalcode
    9. */
    10.  
    11. function getImage($title)
    12. {
    13.   $source = file_get_contents("http://www.imdbapi.com/?i=&t=$title");
    14.   $decode = json_decode($source, true);
    15.   return $decode['Poster'];
    16. }
    17. ?>
    Vick.Kumar, Feb 25, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    17,406
    Likes Received:
    1,740
    Best Answers:
    37
    Trophy Points:
    510
    #2
    Have you tested it? Does it work or has IMDB changed since this code was valid?

    there are lots of code on mediafire - how does this rate compared to other IMDB scripts?

    And why have you posted it? Is it a personal recommendation, are you hoping someone might be overcome with gratitude and give you a "like"?
    sarahk, Feb 25, 2013 IP
  3. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    137
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    65
    #3
    Yeah man. It works !, and like I said I posted it cause I thought it would be useful to share, and if not go ahead and delete it, your the queen.
    Vick.Kumar, Feb 26, 2013 IP
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,703
    Likes Received:
    48
    Best Answers:
    9
    Trophy Points:
    100
    #4
    In case anybody couldn't get the above to work;

    PHP:
    1. <?php
    2. $homepage = file_get_contents('http://www.imdbapi.com/?t=The%20Holiday');
    3. $arr = json_decode($homepage, true);
    4. print($arr['Poster']);
    5. ?>
    To see all the data you can grab:

    PHP:
    1. <?php
    2. $homepage = file_get_contents('http://www.imdbapi.com/?t=The%20Holiday');
    3. $arr = json_decode($homepage, true);
    4. print($homepage);
    5. ?>
    scottlpool2003, Feb 27, 2013 IP
  5. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    137
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    65
    #5
    ^ In regards to that. Awesome ! Also, if you do use it and you want to do bulk processing I recommend using CURL instead of file_get_contents !
    Vick.Kumar, Feb 27, 2013 IP
  6. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,703
    Likes Received:
    48
    Best Answers:
    9
    Trophy Points:
    100
    #6
    Yes CURL would probably be better at handling larger amounts of data, here's an example of usage using the URL to get the movie title:

    PHP:
    1. <?php
    2.  
    3.     $title = rawurlencode($_GET['title']);
    4.  
    5.     //optional comment out or delete
    6.     error_reporting(E_ALL);
    7.  
    8.     // The POST URL and parameters
    9.     $request =  'http://www.imdbapi.com/?t='.$title.'&r=XML';
    10.  
    11.     // Get the curl session object
    12.     $session = curl_init($request);
    13.  
    14.     // Set the POST options.
    15.     curl_setopt($session, CURLOPT_HEADER, true);
    16.     curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    17.  
    18.     // Do the POST and then close the session
    19.     $response = curl_exec($session);
    20.     curl_close($session);
    21.  
    22.     // Get HTTP Status code from the response
    23.     $status_code = array();
    24.     preg_match('/\d\d\d/', $response, $status_code);
    25.  
    26.     // Check for errors
    27.     switch( $status_code[0] ) {
    28.         case 200:
    29.             // Success
    30.             break;
    31.         case 503:
    32.             die('Service unavailable. An internal problem prevented us from returning data to you.');
    33.             break;
    34.         case 403:
    35.             die('Forbidden. You do not have permission to access this resource, or are over your rate limit.');
    36.             break;
    37.         case 400:
    38.             // You may want to fall through here and read the specific XML error
    39.             die('Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
    40.             break;
    41.         default:
    42.             die('Your call returned an unexpected HTTP status of:' . $status_code[0]);
    43.     }
    44.  
    45.     // Get the XML from the response, bypassing the header
    46.     if (!($xml = strstr($response, '<?xml'))) {
    47.         $xml = null;
    48.     }
    49.  
    50.     // Output the XML
    51.  
    52.     $movieInfo = simplexml_load_string($xml);
    53.  
    54.  
    55.  
    56.     echo "Title: ".$movieInfo->movie['title']."<br />Poster: ".$movieInfo->movie['poster']."<br />";
    57.  
    58. ?>
    Slightly modified from this example: http://stackoverflow.com/questions/6932838/php-curl-error-after-changing-only-url

    I think another process to actually grab and save the image would be needed as IMDB do not allow embedding on their images so you'll only get the image URL and not be able to do anything with it.

    Its also worth noting that according to their T&C:


    http://www.imdb.com/help/show_article?conditions
    scottlpool2003, Feb 27, 2013 IP
  7. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    137
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    65
    #7
    Thats right, so it's better to use an API provided by a third-party rather than suffer financial disputes with IMDB's law team.
    Vick.Kumar, Feb 27, 2013 IP