예제 #1
0
 /**
  * Search IMDb for titles matching $searchTerms
  * @method search
  * @param string $searchTerms
  * @param array $wantedTypes *optional* imdb types that should be returned. Defaults to returning all types.
  *                            The class constants MOVIE,GAME etc should be used e.g. [imdbsearch::MOVIE, imdbsearch::TV_SERIES]
  * @param int $maxResults *optional* The maximum number of results to retrieve from IMDB. 0 for unlimited. Defaults to mdb_config::$maxresults
  * @return array array of imdb objects
  */
 public function search($searchTerms, $wantedTypes = null, $maxResults = null)
 {
     $page = '';
     $results = array();
     // @TODO remove maxresults? It has no effect on imdb and why would the user want less results than possible?
     if ($maxResults === null) {
         $maxResults = $this->maxresults;
     }
     if ($this->usecache) {
         $this->cache_read(urlencode(strtolower($searchTerms)) . '.search', $page);
     }
     if (!$page) {
         // @TODO pre filter if they only pick one type
         $url = "http://" . $this->imdbsite . "/find?s=tt&q=" . urlencode($searchTerms);
         $page = $this->makeRequest($url);
         if ($this->storecache && $page) {
             $this->cache_write(urlencode(strtolower($searchTerms)) . '.search', $page);
         }
     }
     // Parse & filter results
     if (preg_match_all('!class="result_text"\\s*>\\s*<a href="/title/tt(?<imdbid>\\d{7})/[^>]*>(?<title>.*?)</a>\\s*(\\([^\\d{4}]\\)\\s*)?(\\((?<year>\\d{4})(.*?|)\\)|)(?<type>[^<]*)!ims', $page, $matches, PREG_SET_ORDER)) {
         foreach ($matches as $match) {
             if (count($results) == $maxResults) {
                 break;
             }
             $type = $this->parseTitleType($match['type']);
             if (is_array($wantedTypes) && !in_array($type, $wantedTypes)) {
                 continue;
             }
             $results[] = imdb::fromSearchResult($match['imdbid'], $match['title'], $match['year'], $type, $this);
         }
     }
     return $results;
 }