Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Request.php
1<?php
2
4
9
11{
12 private string $method = 'POST';
13 private ?array $body = null;
14 private Uri $url;
15 private HttpClient $httpClient;
16
17 public function __construct()
18 {
19 $this->httpClient = new HttpClient();
20 $this->httpClient->setVersion('1.0');
21 }
22
23 public function setHeaders(HttpHeaders $headers): void
24 {
25 $this->httpClient->setHeaders($headers->toArray());
26 }
27
28 public function setUrl(Uri $url): void
29 {
30 $this->url = $url;
31 }
32
33 public function setMethod(string $method): void
34 {
35 $this->method = $method;
36 }
37
38 public function setProxy(array $proxyData): void
39 {
40 if (
41 isset($proxyData['host'])
42 && !empty($proxyData['host'])
43 && isset($proxyData['port'])
44 && !empty($proxyData['port'])
45 ) {
46 $this->httpClient->setProxy(
47 $proxyData['host'],
48 $proxyData['port'],
49 $proxyData['user'] ?? null,
50 $proxyData['password'] ?? null,
51 );
52 }
53 }
54
55 public function setBody(array $body): void
56 {
57 $this->body = $body;
58 }
59
64 public function send(): string
65 {
66 $this->url->addParams($this->body);
67 $isSuccess = $this->httpClient->query(
68 $this->method,
69 $this->url->getLocator(),
70 $this->body
71 );
72
73 if ($isSuccess)
74 {
75 return $this->httpClient->getResult();
76 }
77 else
78 {
79 if ($this->httpClient->getStatus() === 0 && empty($this->httpClient->getError()))
80 {
81 throw new \Exception('Invalid response from host: '.$this->url->getHost().' [RS01]', 400);
82 }
83 else
84 {
85 throw new SystemException(implode('\r\n', $this->httpClient->getError()).' [RS02]');
86 }
87 }
88 }
89}
setProxy(array $proxyData)
Definition Request.php:38
setHeaders(HttpHeaders $headers)
Definition Request.php:23