123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- /**
- * YouTV API PHP client
- */
- class YouTV {
- /**
- * Contains current instance login
- *
- * @var string
- */
- protected $login = '';
- /**
- * Contains current instance password
- *
- * @var string
- */
- protected $password = '';
- /**
- * Contains current instance dealer id
- *
- * @var string
- */
- protected $dealerID = '';
- /**
- * Contains last request status code
- *
- * @var int
- */
- protected $status = 0;
- /**
- * Contains last request error
- *
- * @var int
- */
- protected $error = 0;
- /**
- * Contains current instance temporary token
- *
- * @var string
- */
- protected $token = '';
- /**
- * Contains basic API URL
- *
- * @var string
- */
- protected $url = 'https://api.youtv.com.ua';
- /**
- * Thats constructor. What did you expect there?
- *
- * @param string $login
- * @param string $password
- * @param string $url
- */
- public function __construct($login, $password, $dealerID) {
- $this->dealerID = $dealerID;
- $this->login = $login;
- $this->password = $password;
- // Получим токен
- $this->getToken();
- }
- public function sendRequest($method, $resource, $data = array())
- {
- $url = $this->url . $resource;
- $headers = array(
- "Accept: application/vnd.youtv.v8+json",
- "Device-UUID: 98765432100",
- "Accept-Language: ru"
- );
- if (!empty($this->token)) {
- $headers[] = "Authorization: Bearer " . $this->token;
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- if ($method = 'POST') {
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- $result = curl_exec($ch);
- $response = json_decode($result, true);
- curl_close($ch);
- return $response;
- }
- /**
- * Получаем токен
- */
- private function getToken()
- {
- $data = array(
- 'email' => $this->login,
- 'password' => $this->password
- );
- $response = $this->sendRequest('POST', '/auth/login', $data);
- if (isset($response['token'])) {
- $this->token = $response['token'];
- }
- }
- /**
- * Список тарифов
- *
- * @return mixed
- */
- public function getPrices()
- {
- $resource = '/dealer/' . $this->dealerID . '/prices';
- return $this->sendRequest('GET', $resource);
- }
- /**
- * Создание пользователя.
- *
- * @param $external_id
- * @param $name
- * @param $email
- * @param $password
- * @return mixed
- */
- public function createUser($external_id, $name, $email, $password)
- {
- $resource = '/dealer/' . $this->dealerID . '/users';
- $data = array(
- 'name' => $name,
- 'email' => $email,
- 'external_id' => $external_id,
- 'password' => $password
- );
- return $this->sendRequest('POST', $resource, $data);
- }
- /**
- * Получение всех пользователей дилера
- *
- * @return mixed
- */
- public function getUsers()
- {
- $resource = '/dealer/' . $this->dealerID . '/users';
- return $this->sendRequest('GET', $resource);
- }
- /**
- * Получение абонента
- *
- * @param $user_id
- * @return mixed
- */
- public function getUser($user_id)
- {
- $resource = '/dealer/' . $this->dealerID . '/users/'.$user_id;
- return $this->sendRequest('GET', $resource);
- }
- /**
- * Поиск абонента по external_id
- *
- * @param $user_id
- * @return mixed
- */
- public function getUserByExternalId($external_id)
- {
- $resource = '/dealer/' . $this->dealerID . '/users/external-user-id/'.$external_id;
- return $this->sendRequest('GET', $resource);
- }
- /**
- * Активация подписки для пользователя.
- */
- public function subscriptions($user_id, $price_id, $days = 365)
- {
- $resource = '/dealer/' . $this->dealerID . '/subscriptions';
- $data = array(
- 'user_id' => $user_id,
- 'price_id' => $price_id,
- 'days' => $days
- );
- return $this->sendRequest('POST', $resource, $data);
- }
- /**
- * Блокировка пользователя
- * Результат 1 - всё ок
- * Результат 0 - уже заблокирован
- *
- * @param $user_id
- * @return mixed
- */
- public function blockUser($user_id)
- {
- $resource = '/dealer/' . $this->dealerID . '/users/'.$user_id.'/block';
- return $this->sendRequest('PUT', $resource);
- }
- }
|