Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
http.php
1<?php
2
4
9
14class Http
15{
22 public static function sendRequest(string $url, array $params, array $options = []): Sale\Result
23 {
24 $result = new Sale\Result();
25
26 $httpClientOptions = [];
27 if (array_key_exists('HTTP_CLIENT_OPTIONS', $options) && is_array($options['HTTP_CLIENT_OPTIONS']))
28 {
29 $httpClientOptions = $options['HTTP_CLIENT_OPTIONS'];
30 }
31
32 $httpClient = new HttpClient($httpClientOptions);
33
34 $isJsonRequest = isset($options['JSON_REQUEST']) && $options['JSON_REQUEST'] === true;
35
36 if ($isJsonRequest)
37 {
38 $httpClient->setHeader('Content-Type', 'application/json');
39 }
40
41 $response = $httpClient->post(
42 $url,
43 $isJsonRequest ? Json::encode($params) : $params
44 );
45 if ($response === false)
46 {
47 $errors = $httpClient->getError();
48 foreach ($errors as $code => $message)
49 {
50 $result->addError(new Main\Error($message, $code));
51 }
52
53 return $result;
54 }
55
56 $httpStatus = $httpClient->getStatus();
57 if ($httpStatus === 200)
58 {
59 try
60 {
61 $response = Json::decode($response);
62 $response = array_change_key_case($response, CASE_UPPER);
63 $response = Main\Text\Encoding::convertEncoding($response, 'UTF-8', LANG_CHARSET);
64 }
65 catch (Main\ArgumentException $exception)
66 {
67 $response = [];
68 $result->addError(
69 new Main\Error('Response decoding error', 'RESPONSE_DECODING_ERROR')
70 );
71 }
72
73 $result->setData($response);
74 }
75
76 return $result;
77 }
78}
static decode($data)
Definition json.php:53
static encode($data, $options=null)
Definition json.php:24
static sendRequest(string $url, array $params, array $options=[])
Definition http.php:22