/php/lib/steam/community/WebApi.php

https://github.com/HyperionRiaz/steam-condenser · PHP · 136 lines · 49 code · 19 blank · 68 comment · 11 complexity · 83f33d134ba9bb78b50f80de68420506 MD5 · raw file

  1. <?php
  2. /**
  3. * This code is free software; you can redistribute it and/or modify it under
  4. * the terms of the new BSD License.
  5. *
  6. * Copyright (c) 2010-2011, Sebastian Staudt
  7. *
  8. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  9. */
  10. require_once STEAM_CONDENSER_PATH . 'exceptions/WebApiException.php';
  11. /**
  12. * This adds support for Steam Web API to classes needing this functionality.
  13. * The Web API requires you to register a domain with your Steam account to
  14. * acquire an API key. See http://steamcommunity.com/dev for further details.
  15. *
  16. * @author Sebastian Staudt
  17. * @package steam-condenser
  18. * @subpackage community
  19. */
  20. abstract class WebApi {
  21. /**
  22. * @var string
  23. */
  24. private static $apiKey = null;
  25. /**
  26. * Returns the Steam Web API key
  27. *
  28. * @return string The Steam Web API key
  29. */
  30. public static function getApiKey() {
  31. return self::$apiKey;
  32. }
  33. /**
  34. * Sets the Steam Web API key
  35. *
  36. * @param string $apiKey The 128bit API key that has to be requested from
  37. * http://steamcommunity.com/dev
  38. */
  39. public static function setApiKey($apiKey) {
  40. if($apiKey != null && !preg_match('/^[0-9A-F]{32}$/', $apiKey)) {
  41. throw new WebApiException(WebApiException::INVALID_KEY);
  42. }
  43. self::$apiKey = $apiKey;
  44. }
  45. /**
  46. * Fetches JSON data from Steam Web API using the specified interface,
  47. * method and version. Additional parameters are supplied via HTTP GET.
  48. *
  49. * @param string $interface The Web API interface to call, e.g. ISteamUser
  50. * @param string $method The Web API method to call, e.g.
  51. * GetPlayerSummaries
  52. * @param int $version The API method version to use
  53. * @param array $params Additional parameters to supply via HTTP GET
  54. * @return string Data is returned as a JSON-encoded string.
  55. */
  56. public static function getJSON($interface, $method, $version = 1, $params = null) {
  57. return self::load('json', $interface, $method, $version, $params);
  58. }
  59. /**
  60. * Fetches JSON data from Steam Web API using the specified interface,
  61. * method and version. Additional parameters are supplied via HTTP GET.
  62. *
  63. * @param string $interface The Web API interface to call, e.g. ISteamUser
  64. * @param string $method The Web API method to call, e.g.
  65. * GetPlayerSummaries
  66. * @param int $version The API method version to use
  67. * @param array $params Additional parameters to supply via HTTP GET
  68. * @return stdClass Data is returned as a json_decoded object
  69. */
  70. public static function getJSONData($interface, $method, $version = 1, $params = null) {
  71. $data = self::getJSON($interface, $method, $version, $params);
  72. $result = json_decode($data)->result;
  73. if($result->status != 1) {
  74. throw new WebApiException(WebApiException::STATUS_BAD, $result->status, $result->statusDetail);
  75. }
  76. return $result;
  77. }
  78. /**
  79. * Fetches data from Steam Web API using the specified interface, method
  80. * and version. Additional parameters are supplied via HTTP GET. Data is
  81. * returned as a String in the given format.
  82. *
  83. * @param string $format The format to load from the API ('json', 'vdf', or
  84. * 'xml')
  85. * @param string $interface The Web API interface to call, e.g. ISteamUser
  86. * @param string $method The Web API method to call, e.g.
  87. * GetPlayerSummaries
  88. * @param int $version The API method version to use
  89. * @param array $params Additional parameters to supply via HTTP GET
  90. * @return sssstring Data is returned as a String in the given format (which
  91. * may be 'json', 'vdf' or 'xml').
  92. */
  93. public static function load($format, $interface, $method, $version = 1, $params = null) {
  94. $version = str_pad($version, 4, '0', STR_PAD_LEFT);
  95. $url = "http://api.steampowered.com/$interface/$method/v$version/";
  96. $params['format'] = $format;
  97. $params['key'] = self::$apiKey;
  98. if($params != null && !empty($params)) {
  99. $url .= '?';
  100. $url_params = array();
  101. foreach($params as $k => $v) {
  102. $url_params[] = "$k=$v";
  103. }
  104. $url .= join('&', $url_params);
  105. }
  106. $data = @file_get_contents($url);
  107. if(empty($data)) {
  108. preg_match('/^.* (\d{3}) (.*)$/', $http_response_header[0], $http_status);
  109. if($http_status[1] == 401) {
  110. throw new WebApiException(WebApiException::UNAUTHORIZED);
  111. }
  112. throw new WebApiException(WebApiException::HTTP_ERROR, $http_status[1], $http_status[2]);
  113. }
  114. return $data;
  115. }
  116. }
  117. ?>