Search code examples
phpimagesaveimdb

Saving IMDb posters with php


I'm making a script for myself to list my favorite movies. So I nearly finished it but I cannot save the small poster from IMDb with php. I used the file_put_contents() function, but it's creating a 0kb jpeg file. here is the code that I used:

file_put_contents('./posters/t0120689.jpg', file_get_contents('http://ia.media-imdb.com/images/M/MV5BMTUxMzQyNjA5MF5BMl5BanBnXkFtZTYwOTU2NTY3._V1._SX214_CR0,0,214,314_.jpg'));

Of course it didn't work. Then I used cURL:

$ch = curl_init(
file_put_contents('./posters/t0120689.jpg', file_get_contents('http://ia.media-imdb.com/images/M/MV5BMTUxMzQyNjA5MF5BMl5BanBnXkFtZTYwOTU2NTY3._V1._SX214_CR0,0,214,314_.jpg');
$fp = fopen('./posters/t0120689.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

and it didn't work either. This is the example movie page from IMDb: http://www.imdb.com/title/tt0120689/


Solution

  • Running this worked for me on my Macbook:

    <?php
    file_put_contents('./t0120689.jpg', file_get_contents('http://ia.media-imdb.com/images/M/MV5BMTUxMzQyNjA5MF5BMl5BanBnXkFtZTYwOTU2NTY3._V1._SX214_CR0,0,214,314_.jpg'));
    ?>
    

    If you are hosting this on a server somewhere, the php ini setting allow_url_include needs to be set to on in order for you te get requests outside of your domain.