api.youtv.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * YouTV API PHP client
  4. */
  5. class YouTV {
  6. /**
  7. * Contains current instance login
  8. *
  9. * @var string
  10. */
  11. protected $login = '';
  12. /**
  13. * Contains current instance password
  14. *
  15. * @var string
  16. */
  17. protected $password = '';
  18. /**
  19. * Contains current instance dealer id
  20. *
  21. * @var string
  22. */
  23. protected $dealerID = '';
  24. /**
  25. * Contains last request status code
  26. *
  27. * @var int
  28. */
  29. protected $status = 0;
  30. /**
  31. * Contains last request error
  32. *
  33. * @var int
  34. */
  35. protected $error = 0;
  36. /**
  37. * Contains current instance temporary token
  38. *
  39. * @var string
  40. */
  41. protected $token = '';
  42. /**
  43. * Contains basic API URL
  44. *
  45. * @var string
  46. */
  47. protected $url = 'https://api.youtv.com.ua';
  48. /**
  49. * Thats constructor. What did you expect there?
  50. *
  51. * @param string $login
  52. * @param string $password
  53. * @param string $url
  54. */
  55. public function __construct($login, $password, $dealerID) {
  56. $this->dealerID = $dealerID;
  57. $this->login = $login;
  58. $this->password = $password;
  59. // Получим токен
  60. $this->getToken();
  61. }
  62. public function sendRequest($method, $resource, $data = array())
  63. {
  64. $url = $this->url . $resource;
  65. $headers = array(
  66. "Accept: application/vnd.youtv.v8+json",
  67. "Device-UUID: 98765432100",
  68. "Accept-Language: ru"
  69. );
  70. if (!empty($this->token)) {
  71. $headers[] = "Authorization: Bearer " . $this->token;
  72. }
  73. $ch = curl_init();
  74. curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
  75. curl_setopt($ch, CURLOPT_URL, $url);
  76. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  77. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  78. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  79. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  80. curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
  81. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  82. if ($method = 'POST') {
  83. curl_setopt($ch, CURLOPT_POST, TRUE);
  84. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  85. }
  86. $result = curl_exec($ch);
  87. $response = json_decode($result, true);
  88. curl_close($ch);
  89. return $response;
  90. }
  91. /**
  92. * Получаем токен
  93. */
  94. private function getToken()
  95. {
  96. $data = array(
  97. 'email' => $this->login,
  98. 'password' => $this->password
  99. );
  100. $response = $this->sendRequest('POST', '/auth/login', $data);
  101. if (isset($response['token'])) {
  102. $this->token = $response['token'];
  103. }
  104. }
  105. /**
  106. * Список тарифов
  107. *
  108. * @return mixed
  109. */
  110. public function getPrices()
  111. {
  112. $resource = '/dealer/' . $this->dealerID . '/prices';
  113. return $this->sendRequest('GET', $resource);
  114. }
  115. /**
  116. * Создание пользователя.
  117. *
  118. * @param $external_id
  119. * @param $name
  120. * @param $email
  121. * @param $password
  122. * @return mixed
  123. */
  124. public function createUser($external_id, $name, $email, $password)
  125. {
  126. $resource = '/dealer/' . $this->dealerID . '/users';
  127. $data = array(
  128. 'name' => $name,
  129. 'email' => $email,
  130. 'external_id' => $external_id,
  131. 'password' => $password
  132. );
  133. return $this->sendRequest('POST', $resource, $data);
  134. }
  135. /**
  136. * Получение всех пользователей дилера
  137. *
  138. * @return mixed
  139. */
  140. public function getUsers()
  141. {
  142. $resource = '/dealer/' . $this->dealerID . '/users';
  143. return $this->sendRequest('GET', $resource);
  144. }
  145. /**
  146. * Получение абонента
  147. *
  148. * @param $user_id
  149. * @return mixed
  150. */
  151. public function getUser($user_id)
  152. {
  153. $resource = '/dealer/' . $this->dealerID . '/users/'.$user_id;
  154. return $this->sendRequest('GET', $resource);
  155. }
  156. /**
  157. * Поиск абонента по external_id
  158. *
  159. * @param $user_id
  160. * @return mixed
  161. */
  162. public function getUserByExternalId($external_id)
  163. {
  164. $resource = '/dealer/' . $this->dealerID . '/users/external-user-id/'.$external_id;
  165. return $this->sendRequest('GET', $resource);
  166. }
  167. /**
  168. * Активация подписки для пользователя.
  169. */
  170. public function subscriptions($user_id, $price_id, $days = 365)
  171. {
  172. $resource = '/dealer/' . $this->dealerID . '/subscriptions';
  173. $data = array(
  174. 'user_id' => $user_id,
  175. 'price_id' => $price_id,
  176. 'days' => $days
  177. );
  178. return $this->sendRequest('POST', $resource, $data);
  179. }
  180. /**
  181. * Блокировка пользователя
  182. * Результат 1 - всё ок
  183. * Результат 0 - уже заблокирован
  184. *
  185. * @param $user_id
  186. * @return mixed
  187. */
  188. public function blockUser($user_id)
  189. {
  190. $resource = '/dealer/' . $this->dealerID . '/users/'.$user_id.'/block';
  191. return $this->sendRequest('PUT', $resource);
  192. }
  193. }