I have written a PHP script on my local server to loop through an array of movie titles and, using http://www.imdbapi.com/, pull down associated metadata.
for ($i=0; $i < count($files['Film']); $i++) {
// get data from IMDB based on file title
$imdb_data = json_decode(file_get_contents('http://www.imdbapi.com/?t='.urlencode($files['Film'][$i]['title'])), true);
// save poster image
$img_name = trim(strtolower($imdb_data['Title'])).'.jpg';
file_put_contents('img/posters/'.$img_name, file_get_contents($imdb_data['Poster']));
// set film data
$files['Film'][$i]['year'] = $imdb_data['Year'];
$files['Film'][$i]['runtime'] = $imdb_data['Runtime'];
$files['Film'][$i]['rating'] = $imdb_data['Rating'];
$files['Film'][$i]['summary'] = $imdb_data['Plot'];
$files['Film'][$i]['image'] = $img_name;
$files['Film'][$i]['location_id'] = $this->data['Film']['location_id'];
}
I hit the php max_execution_time on the line that starts file_put_contents. I’ve noticed that a few images do get downloaded and I also get an incomplete image so I’m guessing I hit the time limit when downloading images.
What can I do to improve my script to prevent this? I don’t really like the workaround of increasing the time limit if there’s something fundamental I can to that will optimize the script.
This is mostly a copy paste from a project (loading data from mysql asynchronously) I was working some month ago. I changed it a little bit to your needs, I have put some comments for you, and creted a new file (getData.php).
I haven’t gone exactly in detail with downloading movies, however it downloads an image, and uploads it, pretty much the same process, hope you can adjust it to your needs.
put these two file in a directory and run index.php
This is the firs file: index.php
This is the second file getData.php