There was a plenty of these “IMDb info grabbers” or so called APIs. It’s quite a shame that IMDb in fact doesn’t have an actual API. And all of these “grabbers” now does not work, because IMDb has changed it’s design.
I thought it would be a good idea to write and publish working script since one of WDT blog readers have asked for it (and because I needed one myself :)).

I’m using cURL, since it’s faster than file_get_html() and other similar functions.
Variable $url of course stores URL of IMDb page we will be extracting our content from.
That’s pretty much everything, worth mentioning.

Now – the CODE

  1. <?php
  2.     function curl($url){
  3.             $ch = curl_init();
  4.             curl_setopt($ch, CURLOPT_URL, $url);
  5.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  6.             $result = curl_exec($ch);
  7.             curl_close($ch);
  8.         return $result;
  9.     }
  10.     $url = ‘http://www.imdb.com/title/tt1201607/’;
  11.     $imdb_content = curl($url);
  12.     preg_match(“/<title>(.*)<\/title>/isU”$imdb_content$title);
  13.     $title = str_replace(” – IMDb”“”$title[1]);
  14.         echo “<h1>$title</h1>”;
  15.     preg_match(“/Runtime:<\/h4>(.*)<\/div>/isU”$imdb_content$runtime);
  16.         echo “<h2>Runtime</h2>”.$runtime[1].“”;
  17.     preg_match(“/itemprop=\”ratingValue\”>(.*)<\/span>/isU”$imdb_content$user_rating);
  18.         echo “<h2>Rating</h2>”.$user_rating[1].“”;
  19.     preg_match(‘/Genres:<\/h4>(.*)<\/div>/isU’$imdb_content$genres);
  20.         echo “<h2>Genres</h2>”.str_replace(array(‘ ‘‘ ‘)“”strip_tags($genres[1])).“”;
  21.     preg_match(“/Storyline<\/h2>(.*)<em/isU”$imdb_content$storyline);
  22.         echo “<h2>Storyline</h2>'”.str_replace(array(‘<p>), “”,trim($storyline[1])).”‘“;
  23. ?>

P.S. A little trimming is just for that – trimming(cleaning up a bit) empty spaces. Genres may be easily extracted and manipulated my explode() function.

Happy using, just tell thanks and share!

LEAVE A REPLY

Please enter your comment!
Please enter your name here