cedrea

PHP Class to grab imdb movie info

Aug 26th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <?php
  2.  
  3. class getImdbInfo{
  4.    
  5.     private $id;
  6.     private $initial_data;
  7.    
  8.     private $title;
  9.     private $Year;
  10.     private $Released;
  11.     private $Runtime;
  12.     private $Genre;
  13.     private $Director;
  14.     private $Writer;
  15.     private $Actors;
  16.     private $Plot;
  17.     private $Poster;
  18.     private $imdbRating;
  19.     private $imdbVotes;
  20.    
  21.     function __construct($link){
  22.             if(filter_var($link, FILTER_VALIDATE_URL)){
  23.                 preg_match('(/title/(tt\d{5,7})/)u', $link, $matches, PREG_OFFSET_CAPTURE);
  24.                 $this->id = $matches[1][0];            
  25.             }else{
  26.                 throw new Exception('That doesn\'t look like a valid URL.');
  27.             }
  28.     }
  29.    
  30.     public function getData(){
  31.             $get_movie_info = file_get_contents("http://www.omdbapi.com/?i=".$this->id);
  32.             $this->initial_data = json_decode($get_movie_info);
  33.  
  34.             $this->title = $this->initial_data->Title;
  35.             $this->Year = $this->initial_data->Year;
  36.             $this->Released = $this->initial_data->Released;
  37.             $this->Runtime = $this->initial_data->Runtime;
  38.             $this->Genre = $this->initial_data->Genre;
  39.             $this->Director = $this->initial_data->Director;
  40.             $this->Writer = $this->initial_data->Writer;
  41.             $this->Actors = $this->initial_data->Actors;
  42.             $this->Plot = $this->initial_data->Plot;
  43.             $this->Poster = $this->initial_data->Poster;
  44.             $this->imdbRating = $this->initial_data->imdbRating;
  45.             $this->imdbVotes = $this->initial_data->imdbVotes; 
  46.     }
  47.    
  48.     public function retArr(){
  49.         $movie_info = array(
  50.             'title' => $this->title,
  51.             'year' => $this->Year,
  52.             'release date' => $this->Released,
  53.             'runtime' => $this->Runtime,
  54.             'genres' => $this->Genre,
  55.             'director' => $this->Director,
  56.             'writer' => $this->Writer,
  57.             'actors' => $this->Actors,
  58.             'plot' => $this->Plot,
  59.             'poster' => $this->Poster,
  60.             'rating' => $this->imdbRating,
  61.             'votes' => $this->imdbVotes
  62.         );
  63.         return $movie_info;
  64.     }
  65.    
  66. }
  67.  
  68. //Cum functioneaza?
  69.  
  70. //Preia link-ul imdb prin constructor si-si i-a ID-ul filmului
  71. try{
  72.     $imdb = new getImdbInfo('http://www.imdb.com/title/tt0114746/?ref_=nv_sr_1');
  73. }catch (Exception $e){
  74.     echo $e->getMessage();
  75. }
  76. //Din id-ul filmului preluat in constructor preia informatiile cu ajutorul http://www.omdbapi.com/
  77. $imdb->getData();
  78.  
  79. //creeaza un vector cu toate informatiile filmului
  80. $arr = $imdb->retArr();
  81.  
  82. //parcurgem vectorul si afisam fiecare valoare...
  83. foreach( $arr as $a ){
  84.     echo $a."<br/>";
  85. }
Add Comment
Please, Sign In to add comment