예제 #1
0
파일: GBApi.php 프로젝트: nirkbirk/site
 public static function searchForGames($query, $limit = 10)
 {
     self::validateApiKey();
     //Change these if we need to include more stuff.
     $fieldArr = ['id', 'name', 'original_release_date', 'image', 'api_detail_url', 'deck', 'image', 'platforms'];
     //sanitise query
     $query = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($query))))));
     //generate url
     $url = self::$gb . 'games?api_key=' . self::$apikey . '&format=json' . self::formatFieldList($fieldArr) . '&limit=' . $limit . '&filter=platforms:17|94|152,name:' . $query;
     //Limit to Mac, PC, Linux respectively.
     try {
         $json = self::makeApiCall($url);
     } catch (Exception $e) {
         throw new GBApiException('Error contacting Giantbomb. Please try again later.');
     }
     //QueryDb and try to find the object using the giantbomb unique id
     if ($json->results == null || count($json->results) < 1) {
         throw new GBApiException('No results found. Please try another search.');
     }
     $games = [];
     //initialise as empty array
     foreach ($json->results as $result) {
         $game = GamesQuery::create()->findOneByGbId($result->id);
         if ($game == null) {
             if (!isset($result->original_release_date) || $result->original_release_date == null) {
                 continue;
             }
             //skip if not out yet.
             $game = new Games();
             $game->setGbId($result->id);
             $game->setName(Games::generateUniqueName($result->name, $result->original_release_date));
             $game->setGbUrl($result->api_detail_url);
             $game->setTitle($result->name);
             $game->setDescription($result->deck);
             //We don't care if they don't have thumbs. Catch the exceptions and move on
             try {
                 $game->setGbThumb($result->image->screen_url);
             } catch (Exception $e) {
             }
             try {
                 $game->setGbImage($result->image->medium_url);
             } catch (Exception $e) {
             }
             $game->save();
         }
         //append result to list.
         array_push($games, $game);
     }
     return $games;
 }