Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
client.php
1<?php
2
3
5
6
16
17class Client
18{
19 protected $clientId;
20 protected $clientSecret;
21 protected $serviceUrl;
22 protected $refreshToken;
23
26 const SERVICE_PATH = "/rest/";
27 const B24_APP_GRANT_TYPE = 'refresh_token';
28
30 {
31 $this->clientId = $clientId;
32 $this->clientSecret = $clientSecret;
33 $this->serviceUrl = $serviceUrl;
34 }
35
36 protected function getHttpClient()
37 {
38 return new HttpClient(array(
39 'socketTimeout' => static::HTTP_SOCKET_TIMEOUT,
40 'streamTimeout' => static::HTTP_STREAM_TIMEOUT,
41 ));
42 }
43 protected function getRequestUrl($methodName)
44 {
45 return $this->serviceUrl.self::SERVICE_PATH.$methodName;
46 }
47
48 public function call($methodName, $additionalParams=[])
49 {
50 $result = new Result();
51
52 if($this->clientId && $this->clientSecret)
53 {
54 $httpClient = $this->getHttpClient();
55
56 $additionalParams = $this->prepareRequest($additionalParams);
57
58 LoggerDiag::addMessage('CLIENT_CALL_REQUEST', var_export([
59 'getRequestUrl'=>$this->getRequestUrl($methodName),
60 'additionalParams'=>$additionalParams,
61 ], true));
62
63 $httpResult = $httpClient->post(
64 $this->getRequestUrl($methodName),
65 $additionalParams
66 );
67
68 LoggerDiag::addMessage('CLIENT_CALL_PROCESS_RESULT', var_export([
69 'result'=>$httpResult,
70 'status'=>$httpClient->getStatus()
71 ], true));
72
73 $respons = $this->prepareResponse($httpResult);
74
75 if($respons)
76 {
77 LoggerDiag::addMessage('CLIENT_CALL_PROCESS_RESULT_SUCCESS');
78
79 if(isset($respons['error']))
80 {
81 $result->addError(new Error($respons['error_description'], mb_strtoupper($respons['error'])));
82 LoggerDiag::addMessage('CLIENT_CALL_RESULT_ERROR');
83 }
84 else
85 {
86 $result->setData(['DATA'=>$respons]);
87 LoggerDiag::addMessage('CLIENT_CALL_RESULT_SUCCESS', var_export($respons, true));
88 }
89 }
90 else
91 {
92 $result->addError(new Error('Strange answer from Bitrix Service! '.$httpResult, 'STRANGE_ANSWER'));
93 LoggerDiag::addMessage('CLIENT_CALL_PROCESS_RESULT_ERROR');
94 }
95 }
96 else
97 {
98 $result->addError(new Error('No client credentials for refresh token'));
99 LoggerDiag::addMessage('CLIENT_CALL_CLIENT_ID_EMPTY');
100 }
101
102 return $result;
103 }
104
105 protected function prepareResponse($result)
106 {
107 try
108 {
109 return Json::decode($result);
110 }
111 catch(ArgumentException $e)
112 {
113 return false;
114 }
115 }
116
117 protected function prepareRequest($params)
118 {
119 if(!is_array($params))
120 {
121 $params = array();
122 }
123 else
124 {
125 $params = Encoding::convertEncoding($params, LANG_CHARSET, "utf-8");
126 }
127
128 return $params;
129 }
130
132 {
133 $result = new Result();
134
135 if($refreshToken=='')
136 {
137 $result->addError(new Error('Refresh token is empty'));
138 LoggerDiag::addMessage('CLIENT_REFRESH_TOKEN_EMPTY');
139 }
140
141 if(!$this->clientId || !$this->clientSecret)
142 {
143 $result->addError(new Error('No client credentials for refresh token'));
144 LoggerDiag::addMessage('CLIENT_REFRESH_TOKEN_CLIENT_ID_EMPTY');
145 }
146
147 if($result->isSuccess())
148 {
149 $request = OAuthService::SERVICE_URL.'/oauth/token/'.'?'.http_build_query(
150 [
151 'grant_type'=>self::B24_APP_GRANT_TYPE,
152 'client_id'=>$this->clientId,
153 'client_secret'=>$this->clientSecret,
154 'refresh_token'=>$refreshToken
155 ]);
156
157 LoggerDiag::addMessage('CLIENT_REFRESH_TOKEN_REQUEST', var_export($request,true));
158
159 $httpClient = $this->getHttpClient();
160 $httpResult = $httpClient->get($request);
161
162 LoggerDiag::addMessage('CLIENT_REFRESH_TOKEN_PROCESS_RESULT', var_export([
163 'result'=>$httpResult,
164 'status'=>$httpClient->getStatus()
165 ], true));
166
167 $respons = $this->prepareResponse($httpResult);
168 if($respons)
169 {
170 LoggerDiag::addMessage('CLIENT_REFRESH_TOKEN_PROCESS_RESULT_SUCCESS');
171
172 if(isset($respons['error']))
173 {
174 $result->addError(new Error($respons['error_description'], mb_strtoupper($respons['error'])));
175 LoggerDiag::addMessage('CLIENT_REFRESH_TOKEN_RESULT_ERROR');
176 }
177 else
178 {
179 $result->setData(['DATA'=>$respons]);
180 LoggerDiag::addMessage('CLIENT_REFRESH_TOKEN_RESULT_SUCCESS', var_export($respons, true));
181 }
182 }
183 else
184 {
185 $result->addError(new Error('Strange answer from Bitrix Service! ', 'STRANGE_ANSWER_REFRESH_TOKEN'));
186 LoggerDiag::addMessage('CLIENT_REFRESH_TOKEN_PROCESS_RESULT_ERROR');
187 }
188 }
189
190 return $result;
191 }
192
193 public function checkAccessToken($accessToken)
194 {
195 $result = new Result();
196
197 if(!Loader::includeModule('rest'))
198 $result->addError(new Error('Module REST is not included'));
199
200 if($result->isSuccess())
201 {
202 if(!\Bitrix\Rest\OAuthService::getEngine()->isRegistered())
203 {
204 try
205 {
206 \Bitrix\Rest\OAuthService::register();
207 }
208 catch(\Bitrix\Main\SystemException $e)
209 {
210 $result->addError(new Error('OAuthServiceError', ' OAUTH_SERVICE_ERROR'));
211 }
212 }
213
214 if($result->isSuccess())
215 {
216 $client = \Bitrix\Rest\OAuthService::getEngine()->getClient();
217 $respons = $client->call('app.info', ['auth' => $accessToken]);
218 if(isset($respons['error']))
219 $result->addError(new Error($respons['error_description'], mb_strtoupper($respons['error'])));
220 }
221 }
222 return $result;
223 }
224}
static includeModule($moduleName)
Definition loader.php:69
__construct($clientId, $clientSecret, $serviceUrl)
Definition client.php:29
call($methodName, $additionalParams=[])
Definition client.php:48
static addMessage($messageId, $message='')