예제 #1
0
 public function testLoadWithoutKey()
 {
     WebApi::setApiKey(null);
     $data = 'data';
     $webApi = $this->getMockBuilder('\\SteamCondenser\\Community\\WebApi')->setMethods(['request'])->disableOriginalConstructor()->getMock();
     $webApi->expects($this->once())->method('request')->with('https://api.steampowered.com/interface/method/v2/?test=param&format=json')->will($this->returnValue($data));
     $this->instance->setValue($webApi);
     $this->assertEquals('data', WebApi::load('json', 'interface', 'method', 2, ['test' => 'param']));
 }
 /**
  * Loads the news for the given game with the given restrictions
  *
  * @param int $appId The unique Steam Application ID of the game (e.g. 440
  *        for Team Fortress 2). See
  *        http://developer.valvesoftware.com/wiki/Steam_Application_IDs for
  *        all application IDs
  * @param int $count The maximum number of news to load (default: 5).
  *        There's no reliable way to load all news. Use really a really
  *        great number instead
  * @param int $maxLength The maximum content length of the news (default:
  *        null). If a maximum length is defined, the content of the news
  *        will only be at most <var>maxLength</var> characters long plus an
  *        ellipsis
  * @return array An array of news items for the specified game with the
  *         given options
  */
 public static function getNewsForApp($appId, $count = 5, $maxLength = null)
 {
     $params = array('appid' => $appId, 'count' => $count, 'maxlength' => $maxLength);
     $data = json_decode(WebApi::getJSON('ISteamNews', 'GetNewsForApp', 2, $params));
     $newsItems = array();
     foreach ($data->appnews->newsitems as $newsData) {
         $newsItems[] = new AppNews($appId, $newsData);
     }
     return $newsItems;
 }
 /**
  * Loads the global unlock percentages of all achievements for the given
  * game
  *
  * @param int $appId The unique Steam Application ID of the game (e.g.
  *        <var>440</var> for Team Fortress 2). See
  *        http://developer.valvesoftware.com/wiki/Steam_Application_IDs for
  *        all application IDs
  * @return array The symbolic achievement names with the corresponding
  *         global unlock percentages
  * @throws WebApiException if a request to Steam's Web API fails
  */
 public static function getGlobalPercentages($appId)
 {
     $params = array('gameid' => $appId);
     $data = json_decode(WebApi::getJSON('ISteamUserStats', 'GetGlobalAchievementPercentagesForApp', 2, $params));
     $percentages = array();
     foreach ($data->achievementpercentages->achievements as $achievementData) {
         $percentages[$achievementData->name] = (double) $achievementData->percent;
     }
     return $percentages;
 }
예제 #4
0
 /**
  * Loads the news for the given game with the given restrictions
  *
  * @param int $appId The unique Steam Application ID of the game (e.g. 440
  *        for Team Fortress 2). See
  *        http://developer.valvesoftware.com/wiki/Steam_Application_IDs for
  *        all application IDs
  * @param int $count The maximum number of news to load (default: 5).
  *        There's no reliable way to load all news. Use really a really
  *        great number instead
  * @param int $maxLength The maximum content length of the news (default:
  *        null). If a maximum length is defined, the content of the news
  *        will only be at most <var>maxLength</var> characters long plus an
  *        ellipsis
  * @return array An array of news items for the specified game with the
  *         given options
  */
 public static function getNewsForApp($appId, $count = 5, $maxLength = null)
 {
     $params = ['appid' => $appId, 'count' => $count, 'maxlength' => $maxLength];
     $data = WebApi::getJSONObject('ISteamNews', 'GetNewsForApp', 2, $params);
     $newsItems = [];
     foreach ($data->appnews->newsitems as $newsData) {
         $newsItems[] = new AppNews($appId, $newsData);
     }
     return $newsItems;
 }
 /**
  * Returns all Golden Wrenches
  *
  * @return All Golden Wrenches
  * @throws SteamCondenserException If an error occurs querying the Web API
  *                                 or the Steam Community
  */
 public static function getGoldenWrenches()
 {
     if (self::$goldenWrenches == null) {
         self::$goldenWrenches = array();
         $data = json_decode(WebApi::getJSON('ITFItems_440', 'GetGoldenWrenches', 2));
         foreach ($data->results->wrenches as $wrenchData) {
             self::$goldenWrenches[] = new TF2GoldenWrench($wrenchData);
         }
     }
     return self::$goldenWrenches;
 }
 /**
  * Runs this query by calling result() and returns the specified fields of the matching entities by calling WebApi::get for the query results.
  * @param array $fields An array of strings specifying which fields should be returned. The strings in the array have to be UTF-8 encoded.
  * @param bool $return_ids Whether to return ids instead of values for relations
  * @return array A two-dimensional array that contains the results. The keys of the first level are
  * the ids of the entities. The keys of the second level are the field identifiers and values are
  * the corresponding field values. The field values are UTF-8 encoded strings.
  * @throws \SoapFault if a remote error occurs.
  * @see WebApi::get
  */
 public function fields(array $fields, $return_ids = false)
 {
     $queryResults = $this->result();
     $fieldResults = $this->webapi->get($this->type, $queryResults, $fields, $return_ids);
     uksort($fieldResults, function ($id1, $id2) use($queryResults) {
         foreach ($queryResults as $queryResult) {
             if ($queryResult->id == $id1) {
                 return -1;
             }
             if ($queryResult->id == $id2) {
                 return 1;
             }
         }
     });
     return $fieldResults;
 }
예제 #7
0
 /**
  * Resolves a vanity URL of a Steam Community profile to a 64bit numeric
  * SteamID
  *
  * @param string $vanityUrl The vanity URL of a Steam Community profile
  * @return string The 64bit SteamID for the given vanity URL
  * @throws WebApiException if the request to Steam's Web API fails
  */
 public static function resolveVanityUrl($vanityUrl)
 {
     $params = array('vanityurl' => $vanityUrl);
     $json = WebApi::getJSON('ISteamUser', 'ResolveVanityURL', 1, $params);
     $result = json_decode($json);
     $result = $result->response;
     if ($result->success != 1) {
         return null;
     }
     return $result->steamid;
 }
 /**
  * Returns the overall number of players currently playing this game
  *
  * @return int The number of players playing this game
  */
 public function getPlayerCount()
 {
     $params = array('appid' => $this->appId);
     $result = WebApi::getJSON('ISteamUserStats', 'GetNumberOfCurrentPlayers', 1, $params);
     $result = json_decode($result);
     return $result->response->player_count;
 }
 protected function fetchLanguage($language)
 {
     $params = ['appid' => $this->appId, 'l' => $language];
     $data = WebApi::getJSONObject('ISteamUserStats', 'GetSchemaForGame', 2, $params);
     return $data->game;
 }
 /**
  * Updates the item definitions of this schema using the Steam Web API
  *
  * @throws \SteamCondenser\Exceptions\WebApiException if the item schema
  *         cannot be fetched
  */
 public function internalFetch()
 {
     $params = ['language' => $this->language];
     $data = WebApi::getJSONData("IEconItems_{$this->appId}", 'GetSchema', 1, $params);
     $this->attributes = [];
     foreach ($data->attributes as $attribute) {
         $this->attributes[$attribute->defindex] = $attribute;
         $this->attributes[$attribute->name] = $attribute;
     }
     $this->effects = [];
     foreach ($data->attribute_controlled_attached_particles as $effect) {
         $this->effects[$effect->id] = $effect;
     }
     $this->items = [];
     $this->itemNames = [];
     foreach ($data->items as $item) {
         $this->items[$item->defindex] = $item;
         $this->itemNames[$item->name] = $item->defindex;
     }
     if (!empty($data->levels)) {
         $this->itemLevels = [];
         foreach ($data->item_levels as $itemLevelType) {
             $itemLevels = [];
             foreach ($itemLevelType->levels as $itemLevel) {
                 $itemLevels[$itemLevel->level] = $itemLevel->name;
             }
             $this->itemLevels[$itemLevelType->name] = $itemLevels;
         }
     }
     $this->itemSets = [];
     foreach ($data->item_sets as $itemSet) {
         $this->itemSets[$itemSet->item_set] = $itemSet;
     }
     $this->origins = [];
     foreach ($data->originNames as $origin) {
         $this->origins[$origin->origin] = $origin->name;
     }
     $this->qualities = [];
     $index = -1;
     foreach ($data->qualities as $key => $value) {
         $index++;
         if (property_exists($data->qualityNames, $key)) {
             $qualityName = $data->qualityNames->{$key};
         }
         if (empty($qualityName)) {
             $qualityName = ucwords($key);
         }
         $this->qualities[$index] = $qualityName;
     }
 }
 /**
  * Downloads the contents of the specified document
  * @param int $id The id of the document
  * @param int $width The width to which an image should be resized
  * @param int $height The height to which an image should be resized
  * @param string $resizeMode Specifies how an image should be resized
  * @param int $ifModifiedSince If specified, the file is only downloaded if it was modified since <code>$ifModifiedSince</code> (timestamp)
  * @throws Exception If an error occurs during download
  */
 private function download($id, $width, $height, $resizeMode, $ifModifiedSince = null)
 {
     // Build uri
     $uri = new UriBuilder($this->getFileUrl);
     $uri->addParameter("host", $this->connection->host);
     $uri->addParameter("port", $this->connection->port);
     $uri->addParameter("username", $this->connection->username);
     $uri->addParameter("password", $this->connection->password);
     $uri->addParameter("id", $id);
     if (isset($width)) {
         $uri->addParameter("width", $width);
     }
     if (isset($height)) {
         $uri->addParameter("height", $height);
     }
     if (isset($resizeMode)) {
         $uri->addParameter("resizeMode", $resizeMode);
     }
     // Begin request
     if (isset($ifModifiedSince) && is_int($ifModifiedSince)) {
         // If $ifModifiedSince is specified, set the If-Modified-Since header
         $header = 'If-Modified-Since: ' . date('r', $ifModifiedSince);
         $this->webapi->debug("FileOperation::download - Adding header ", $header);
         $source = fopen($uri, 'rb', false, stream_context_create(array('http' => array('header' => $header))));
     } else {
         $source = fopen($uri, 'rb');
     }
     $this->webapi->debug("FileOperation::download - Requesting ", (string) $uri);
     if (!$source) {
         throw new \Exception('Could not open ' . $uri);
     }
     // Get content-type and file extension from stream meta-data (http-headers)
     $meta = stream_get_meta_data($source);
     if (isset($meta['wrapper_data']) && is_array($meta['wrapper_data'])) {
         $this->webapi->debug("FileOperation::download - Headers: ", $meta['wrapper_data']);
         foreach ($meta['wrapper_data'] as $header) {
             if (stripos($header, 'HTTP/1.1 304') === 0) {
                 // Not modified: Return immediately
                 $this->webapi->debug("FileOperation::download - Retrieved status: 304 Not Modified");
                 fclose($source);
                 return;
             }
             if (stripos($header, 'content-type:') === 0) {
                 $contentType = substr($header, strpos($header, ':') + 2);
             }
             if (stripos($header, 'last-modified:') === 0) {
                 $lastModified = strtotime(substr($header, strpos($header, ':') + 2));
             } else {
                 $lastModified = time();
             }
             if (stripos($header, 'content-disposition:') === 0) {
                 $contentDisposition = substr($header, strpos($header, ':') + 1);
                 $pos = stripos($contentDisposition, 'filename=');
                 if ($pos !== false) {
                     $filename = substr($contentDisposition, $pos + strlen('filename='));
                     if (strpos($filename, '"') === 0) {
                         $filename = substr($filename, 1, strlen($filename) - 2);
                     }
                     $extension = strtolower(substr($filename, strrpos($filename, '.')));
                 }
             }
         }
     }
     if (!isset($contentType)) {
         throw new \Exception('Could not get content-type from ' . $uri);
     }
     if (!isset($extension)) {
         throw new \Exception('Could not get file extension from ' . $uri);
     }
     $this->webapi->debug("FileOperation::download - Got content-type: ", $contentType);
     $this->webapi->debug("FileOperation::download - Got file extension: ", $extension);
     $this->webapi->debug("FileOperation::download - Got last-modified: ", $lastModified);
     $this->processMetaData($id, $width, $height, $resizeMode, $contentType, $filename, $extension, $lastModified);
     // Process file data
     while (!feof($source)) {
         $data = fread($source, $this->bufferSize);
         $this->process($data);
     }
     // Close file handle
     fclose($source);
 }
예제 #12
0
 /**
  * Sets the Steam Web API key
  *
  * @param string $apiKey The 128bit API key that has to be requested from
  *                      http://steamcommunity.com/dev
  * @throws WebApiException if the given API key is not a valid 128bit
  *         hexadecimal string
  */
 public static function setApiKey($apiKey)
 {
     if ($apiKey != null && !preg_match('/^[0-9A-F]{32}$/', $apiKey)) {
         throw new WebApiException(WebApiException::INVALID_KEY);
     }
     self::$apiKey = $apiKey;
 }
 /**
  * Updates the contents of the backpack using Steam Web API
  */
 public function fetch()
 {
     $params = array('SteamID' => $this->steamId64);
     $result = WebApi::getJSONData("IEconItems_{$this->getAppId()}", 'GetPlayerItems', 1, $params);
     $this->items = array();
     $this->preliminaryItems = array();
     foreach ($result->items as $itemData) {
         if ($itemData != null) {
             $inventoryClass = new ReflectionClass(get_class($this));
             $itemClass = $inventoryClass->getConstant('ITEM_CLASS');
             $item = new $itemClass($this, $itemData);
             if ($item->isPreliminary()) {
                 $this->preliminaryItems[] = $item;
             } else {
                 $this->items[$item->getBackpackPosition() - 1] = $item;
             }
         }
     }
     $this->fetchDate = time();
 }