1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
bitrixservice.php
См. документацию.
1
<?php
2
use
Bitrix\Main\Web\HttpClient
;
3
use
Bitrix\Main\Web\Json
;
4
5
class
CBitrixServiceOAuthInterface
extends
CSocServOAuthTransport
6
{
7
const
SERVICE_ID
=
"bitrixgeneric"
;
8
9
const
AUTH_URL
=
"/oauth/authorize/"
;
10
const
TOKEN_URL
=
"/oauth/token/"
;
11
12
const
URL
=
''
;
13
14
protected
$scope
=
array
();
15
16
protected
$authResult
=
array
();
17
18
public
function
__construct
(
$appID
=
false
,
$appSecret
=
false
,
$code
=
false
)
19
{
20
parent::__construct(
$appID
,
$appSecret
,
$code
);
21
}
22
23
public
function
getResult
()
24
{
25
return
$this->authResult
;
26
}
27
28
public
function
getError
()
29
{
30
return
is_array($this->authResult) && isset($this->authResult[
'error'
])
31
? $this->authResult
32
:
''
;
33
}
34
}
35
36
class
CBitrixServiceTransport
37
{
38
const
SERVICE_URL
=
"/rest/"
;
39
40
const
METHOD_METHODS
=
'methods'
;
41
const
METHOD_BATCH
=
'batch'
;
42
43
protected
$clientId
=
''
;
44
protected
$clientSecret
=
''
;
45
protected
$httpTimeout
=
SOCSERV_DEFAULT_HTTP_TIMEOUT
;
46
protected
?
int
$streamTimeout
=
null
;
47
protected
bool
$listenHttpErrors
=
false
;
48
49
protected
$serviceHost
=
''
;
50
51
public
function
__construct
(
$clientId
,
$clientSecret
)
52
{
53
$this->clientId =
$clientId
;
54
$this->clientSecret =
$clientSecret
;
55
}
56
57
protected
function
setSeviceHost
(
$host
)
58
{
59
$this->serviceHost =
$host
;
60
}
61
62
protected
function
prepareAnswer
(
$result
)
63
{
64
return
Json::decode(
$result
);
65
}
66
67
public
function
call
($methodName, $additionalParams =
null
, $licenseCheck =
false
)
68
{
69
global
$APPLICATION
;
70
71
if
($this->clientId && $this->clientSecret)
72
{
73
if
(!is_array($additionalParams))
74
{
75
$additionalParams =
array
();
76
}
77
78
$additionalParams[
'client_id'
] =
$this->clientId
;
79
$additionalParams[
'client_secret'
] =
$this->clientSecret
;
80
81
if
($licenseCheck)
82
{
83
$additionalParams[
'key'
] = static::getLicense();
84
}
85
86
$httpClientParams = [
87
'socketTimeout'
=>
$this->httpTimeout
,
88
];
89
90
if
($this->streamTimeout)
91
{
92
$httpClientParams[
'streamTimeout'
] =
$this->streamTimeout
;
93
}
94
95
$http =
new
HttpClient
($httpClientParams);
96
$result
= $http->post(
97
$this->serviceHost.static::SERVICE_URL.$methodName,
98
$additionalParams
99
);
100
101
$res
=
false
;
102
103
try
104
{
105
$res
= $this->
prepareAnswer
(
$result
);
106
}
107
catch
(\
Bitrix
\Main\ArgumentException $e)
108
{
109
110
}
111
112
if
($this->
listenHttpErrors
&& !
$res
&& $http->getError())
113
{
114
$res
= [
115
'error'
=> $this->parseHttpError($http->getError())
116
];
117
}
118
119
if
(
$res
)
120
{
121
if
(!$licenseCheck && is_array(
$res
) && isset(
$res
[
'error'
]) &&
$res
[
'error'
] ===
'verification_needed'
)
122
{
123
return
$this->
call
($methodName, $additionalParams,
true
);
124
}
125
}
126
else
127
{
128
AddMessage2Log
(
'Strange answer from Bitrix Service! '
.$this->serviceHost.static::SERVICE_URL.$methodName.
": "
.$http->getStatus().
' '
.
$result
);
129
}
130
131
return
$res
;
132
}
133
else
134
{
135
throw
new \Bitrix\Main\SystemException(
"No client credentials"
);
136
}
137
}
138
145
private
function
parseHttpError(
array
$errors
): string
146
{
147
$errorsMsg = [];
148
foreach
(
$errors
as
$key
=> $value)
149
{
150
if
(is_string(
$key
))
151
{
152
$errorsMsg[] =
"[{$key}] {$value}"
;
153
}
154
else
155
{
156
$errorsMsg[] =
$value
;
157
}
158
}
159
160
return
implode(
', '
, $errorsMsg);
161
}
162
163
public
function
batch
($actions)
164
{
165
$batch =
array
();
166
167
if
(is_array($actions))
168
{
169
foreach
($actions as $query_key => $arCmd)
170
{
171
list($cmd,
$arParams
) = array_values($arCmd);
172
$batch[
'cmd'
][$query_key] = $cmd.(is_array(
$arParams
) ?
'?'
.http_build_query(
$arParams
) :
''
);
173
}
174
}
175
176
return
$this->
call
(static::METHOD_BATCH, $batch);
177
}
178
179
public
function
getMethods
()
180
{
181
return
$this->
call
(self::METHOD_METHODS);
182
}
183
184
public
function
setTimeout
($timeout)
185
{
186
$this->httpTimeout = $timeout;
187
}
188
189
public
function
setStreamTimeout
(
int
$streamTimeout
): void
190
{
191
$this->streamTimeout =
$streamTimeout
;
192
}
193
202
public
function
listenHttpErrors
(
bool
$listen =
true
): static
203
{
204
$this->
listenHttpErrors
= $listen;
205
206
return
$this;
207
}
208
209
protected
static
function
getLicense
()
210
{
211
return
md5(
LICENSE_KEY
);
212
}
213
}
$arParams
$arParams
Определения
access_dialog.php:21
$APPLICATION
global $APPLICATION
Определения
include.php:80
Bitrix\Main\Web\HttpClient
Определения
httpclient.php:24
Bitrix\Main\Web\Json
Определения
json.php:9
CBitrixServiceOAuthInterface
Определения
bitrixservice.php:6
CBitrixServiceOAuthInterface\__construct
__construct($appID=false, $appSecret=false, $code=false)
Определения
bitrixservice.php:18
CBitrixServiceOAuthInterface\TOKEN_URL
const TOKEN_URL
Определения
bitrixservice.php:10
CBitrixServiceOAuthInterface\getError
getError()
Определения
bitrixservice.php:28
CBitrixServiceOAuthInterface\URL
const URL
Определения
bitrixservice.php:12
CBitrixServiceOAuthInterface\$scope
$scope
Определения
bitrixservice.php:14
CBitrixServiceOAuthInterface\AUTH_URL
const AUTH_URL
Определения
bitrixservice.php:9
CBitrixServiceOAuthInterface\SERVICE_ID
const SERVICE_ID
Определения
bitrixservice.php:7
CBitrixServiceOAuthInterface\$authResult
$authResult
Определения
bitrixservice.php:16
CBitrixServiceOAuthInterface\getResult
getResult()
Определения
bitrixservice.php:23
CBitrixServiceTransport
Определения
bitrixservice.php:37
CBitrixServiceTransport\$httpTimeout
$httpTimeout
Определения
bitrixservice.php:45
CBitrixServiceTransport\$clientSecret
$clientSecret
Определения
bitrixservice.php:44
CBitrixServiceTransport\setTimeout
setTimeout($timeout)
Определения
bitrixservice.php:184
CBitrixServiceTransport\listenHttpErrors
listenHttpErrors(bool $listen=true)
Определения
bitrixservice.php:202
CBitrixServiceTransport\$serviceHost
$serviceHost
Определения
bitrixservice.php:49
CBitrixServiceTransport\setSeviceHost
setSeviceHost($host)
Определения
bitrixservice.php:57
CBitrixServiceTransport\METHOD_METHODS
const METHOD_METHODS
Определения
bitrixservice.php:40
CBitrixServiceTransport\$clientId
$clientId
Определения
bitrixservice.php:43
CBitrixServiceTransport\call
call($methodName, $additionalParams=null, $licenseCheck=false)
Определения
bitrixservice.php:67
CBitrixServiceTransport\__construct
__construct($clientId, $clientSecret)
Определения
bitrixservice.php:51
CBitrixServiceTransport\METHOD_BATCH
const METHOD_BATCH
Определения
bitrixservice.php:41
CBitrixServiceTransport\$streamTimeout
int $streamTimeout
Определения
bitrixservice.php:46
CBitrixServiceTransport\getLicense
static getLicense()
Определения
bitrixservice.php:209
CBitrixServiceTransport\setStreamTimeout
setStreamTimeout(int $streamTimeout)
Определения
bitrixservice.php:189
CBitrixServiceTransport\getMethods
getMethods()
Определения
bitrixservice.php:179
CBitrixServiceTransport\SERVICE_URL
const SERVICE_URL
Определения
bitrixservice.php:38
CBitrixServiceTransport\batch
batch($actions)
Определения
bitrixservice.php:163
CBitrixServiceTransport\$listenHttpErrors
bool $listenHttpErrors
Определения
bitrixservice.php:47
CBitrixServiceTransport\prepareAnswer
prepareAnswer($result)
Определения
bitrixservice.php:62
CSocServOAuthTransport
Определения
oauthtransport.php:3
CSocServOAuthTransport\$appSecret
$appSecret
Определения
oauthtransport.php:7
CSocServOAuthTransport\$code
$code
Определения
oauthtransport.php:8
CSocServOAuthTransport\$appID
$appID
Определения
oauthtransport.php:6
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
$res
$res
Определения
filter_act.php:7
$result
$result
Определения
get_property_values.php:14
$host
$host
Определения
.description.php:9
$errors
$errors
Определения
iblock_catalog_edit.php:74
LICENSE_KEY
const LICENSE_KEY($show_sql_stat=='Y')
Определения
start.php:84
AddMessage2Log
AddMessage2Log($text, $module='', $traceDepth=6, $showArgs=false)
Определения
tools.php:3941
Bitrix\Im\V2\Chat\Param\$value
$value
Определения
Param.php:39
Bitrix
$key
if(empty($signedUserToken)) $key
Определения
quickway.php:257
SOCSERV_DEFAULT_HTTP_TIMEOUT
const SOCSERV_DEFAULT_HTTP_TIMEOUT
Определения
include.php:5
bitrix
modules
socialservices
classes
general
bitrixservice.php
Создано системой
1.14.0