api.prostotv.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace UTG;
  3. /**
  4. * ProstoTV API PHP client
  5. */
  6. class ProstoTV {
  7. /**
  8. * Contains current instance login
  9. *
  10. * @var string
  11. */
  12. protected $login = '';
  13. /**
  14. * Contains current instance password
  15. *
  16. * @var string
  17. */
  18. protected $password = '';
  19. /**
  20. * Contains last request status code
  21. *
  22. * @var int
  23. */
  24. protected $status = 0;
  25. /**
  26. * Contains last request error
  27. *
  28. * @var int
  29. */
  30. protected $error = 0;
  31. /**
  32. * Contains current instance temporary token
  33. *
  34. * @var string
  35. */
  36. protected $token = '';
  37. /**
  38. * Contains basic API URL
  39. *
  40. * @var string
  41. */
  42. protected $url = 'https://api.prosto.tv/v1/';
  43. /**
  44. * Thats constructor. What did you expect there?
  45. *
  46. * @param string $login
  47. * @param string $password
  48. * @param string $url
  49. */
  50. public function __construct($login, $password, $url = null) {
  51. if ($url) {
  52. $this->url = $url;
  53. }
  54. $this->login = $login;
  55. $this->password = $password;
  56. $this->token = null;
  57. }
  58. /**
  59. * Magic getter for last request status or error properties
  60. *
  61. * @param string $name
  62. *
  63. * @return int
  64. */
  65. public function __get($name) {
  66. switch ($name) {
  67. case 'status':
  68. return ($this->status);
  69. break;
  70. case 'error':
  71. return ($this->error);
  72. break;
  73. }
  74. }
  75. /**
  76. * Performs some GET request to remote API
  77. *
  78. * @param string $resource
  79. *
  80. * @return string/bool
  81. */
  82. public function get($resource) {
  83. return ($this->request('GET', $resource));
  84. }
  85. /**
  86. * Performs some POST request to remote API
  87. *
  88. * @param string $resource
  89. * @param array $data
  90. *
  91. * @return string
  92. */
  93. public function post($resource, $data = array()) {
  94. return ($this->request('POST', $resource, $data));
  95. }
  96. /**
  97. * Performs PUT request to some remote API resource
  98. *
  99. * @param string $resource
  100. * @param array $data
  101. *
  102. * @return string
  103. */
  104. public function put($resource, $data = array()) {
  105. return ($this->request('PUT', $resource, $data));
  106. }
  107. /**
  108. * Performs DELETE request to some remote API resource
  109. *
  110. * @param string $resource
  111. *
  112. * @return string
  113. */
  114. public function delete($resource) {
  115. return ($this->request('DELETE', $resource));
  116. }
  117. /**
  118. * Pushes request of some specified method to remote API
  119. *
  120. * @param string $method
  121. * @param string $resource
  122. * @param array $data
  123. *
  124. * @return array/boolean on error
  125. */
  126. protected function request($method, $resource, $data = array()) {
  127. if (!$this->token && ($method != 'POST' || $resource != '/tokens')) {
  128. $this->getToken();
  129. }
  130. $context = array('http' => array(
  131. 'method' => $method,
  132. 'header' => array("Content-Type: application/json; charset=utf-8"),
  133. 'ignore_errors' => true,
  134. 'timeout' => 60,
  135. ));
  136. if ($this->token) {
  137. $context['http']['header'][] = "Authorization: Bearer " . $this->token;
  138. }
  139. if ($method != 'GET') {
  140. $context['http']['content'] = json_encode($data);
  141. }
  142. $context = stream_context_create($context);
  143. try {
  144. $content = file_get_contents($this->url . ltrim($resource, '/ '), false, $context);
  145. $content = json_decode($content, true);
  146. } catch (Exception $e) {
  147. $this->error = $e;
  148. return (false);
  149. }
  150. $answer = explode(' ', $http_response_header[0]);
  151. $this->status = intval($answer[1]);
  152. if (in_array($this->status, array(200, 201))) {
  153. $this->error = null;
  154. return ($content);
  155. } else {
  156. $this->error = $content;
  157. return (false);
  158. }
  159. }
  160. /**
  161. * Sets temporary token to current instance
  162. *
  163. * @return void
  164. */
  165. protected function getToken() {
  166. $data = $this->request('POST', '/tokens', array('login' => $this->login, 'password' => $this->password));
  167. $this->token = $data['token'];
  168. }
  169. }