Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
client.php
1<?php
3
4
7
8class Client
9{
10 private $httpClient;
11 protected $accessTokenUrl;
12 protected $clientId;
13 protected $clientSecret;
14
15 public function __construct(array $options = [])
16 {
17 foreach ($options as $option => $value)
18 {
19 if (property_exists(self::class, $option))
20 {
21 $this->{$option} = $value;
22 }
23 }
24
25 $this->httpClient = new HttpClient();
26 $this->httpClient->setHeader("User-Agent", "Bitrix IntegrationB24");
27 $this->httpClient->setCharset("UTF-8");
28 }
29
30 public function getAccessTokenUrl(array $params)
31 {
32 $url = $this->getBaseAccessTokenUrl();
33 if ($this->getAccessTokenMethod() === HttpClient::HTTP_GET)
34 {
35 $query = http_build_query($params, null, "&");
36 return $url."?".$query;
37 }
38
39 return $url;
40 }
41
48 public function getAccessToken($grant, array $options = [])
49 {
50 $params = [
51 "grant_type" => $grant,
52 "client_id" => $this->getClientId(),
53 "client_secret" => $this->getClientSecret(),
54 ];
55
56 $params = array_merge($params, $options);
57 $success = $this->httpClient->query($this->getAccessTokenMethod(), $this->getAccessTokenUrl($params));
58
59 $response = [];
60 if ($success)
61 {
62 try
63 {
64 $response = Json::decode($this->httpClient->getResult());
65 }
66 catch (\Bitrix\Main\ArgumentException $exception)
67 {
68
69 }
70 }
71
72 return $response;
73 }
74
75 protected function getAccessTokenMethod()
76 {
77 return HttpClient::HTTP_GET;
78 }
79
80 protected function getBaseAccessTokenUrl()
81 {
83 }
84
85 protected function getClientId()
86 {
87 return $this->clientId;
88 }
89
90 protected function getClientSecret()
91 {
93 }
94}
getAccessToken($grant, array $options=[])
Definition client.php:48