ILIAS  release_8 Revision v8.23
HttpMessage.php
Go to the documentation of this file.
1 <?php
2 
20 
22 
31 {
37  public bool $ok = false;
38 
39  //UK:added
40  public object $responseJson;
41 
47  public ?string $request = null;
48 
54  public $requestHeaders = '';
55 
61  public ?string $response = null;
62 
68  public $responseHeaders = '';
69 
75  public array $relativeLinks = array();
76 
82  public int $status = 0;
83 
89  public string $error = '';
90 
96  private ?string $url = null;
97 
103  private ?string $method = null;
104 
110  private static ?ClientInterface $httpClient = null; //changed ...= null
111 
119  public function __construct(string $url, string $method = 'GET', $params = null, string $header = null)
120  {
121  $this->url = $url;
122  $this->method = strtoupper($method);
123  if (is_array($params)) {
124  $this->request = http_build_query($params);
125  } else {
126  $this->request = $params;
127  }
128  if (!empty($header)) {
129  $this->requestHeaders = explode("\n", $header);
130  }
131  }
132 
138  public function getUrl(): ?string
139  {
140  return $this->url;
141  }
142 
148  public function getMethod(): ?string
149  {
150  return $this->method;
151  }
152 
158  public static function setHttpClient(ClientInterface $httpClient = null)
159  {
160  self::$httpClient = $httpClient;
161  Util::logDebug('HttpClient set to \'' . get_class(self::$httpClient) . '\'');
162  }
163 
169  public static function getHttpClient()
170  {
171  if (!self::$httpClient) {
172  if (function_exists('curl_init')) {
173  self::$httpClient = new CurlClient();
174  } elseif (ini_get('allow_url_fopen')) {
175  self::$httpClient = new StreamClient();
176  }
177  if (self::$httpClient) {
178  Util::logDebug('HttpClient set to \'' . get_class(self::$httpClient) . '\'');
179  }
180  }
181 
182  return self::$httpClient;
183  }
184 
190  public function send(): bool
191  {
192  $client = self::getHttpClient();
193  $this->relativeLinks = array();
194  if (empty($client)) {
195  $this->ok = false;
196  $message = 'No HTTP client interface is available';
197  $this->error = $message;
198  Util::logError($message, true);
199  } elseif (empty($this->url)) {
200  $this->ok = false;
201  $message = 'No URL provided for HTTP request';
202  $this->error = $message;
203  Util::logError($message, true);
204  } else {
205  $this->ok = $client->send($this);
206  $this->parseRelativeLinks();
208  $message = "Http\\HttpMessage->send {$this->method} request to '{$this->url}'";
209  if (!empty($this->requestHeaders)) {
210  $message .= "\n{$this->requestHeaders}";
211  }
212  if (!empty($this->request)) {
213  $message .= "\n\n{$this->request}";
214  }
215  $message .= "\nResponse:";
216  if (!empty($this->responseHeaders)) {
217  $message .= "\n{$this->responseHeaders}";
218  }
219  if (!empty($this->response)) {
220  $message .= "\n\n{$this->response}";
221  }
222  if ($this->ok) {
224  } else {
225  if (!empty($this->error)) {
226  $message .= "\nError: {$this->error}";
227  }
229  }
230  }
231  }
232 
233  return $this->ok;
234  }
235 
241  public function hasRelativeLink(string $rel): bool
242  {
243  return array_key_exists($rel, $this->relativeLinks);
244  }
245 
251  public function getRelativeLink(string $rel): ?string
252  {
253  $url = null;
254  if ($this->hasRelativeLink($rel)) {
255  $url = $this->relativeLinks[$rel];
256  }
257 
258  return $url;
259  }
260 
266  public function getRelativeLinks(): array
267  {
268  return $this->relativeLinks;
269  }
270 
271  ###
272  ### PRIVATE METHOD
273  ###
274 
278  private function parseRelativeLinks()
279  {
280  $matched = preg_match_all('/^(Link|link): *(.*)$/m', $this->responseHeaders, $matches);
281  if ($matched) {
282  for ($i = 0; $i < $matched; $i++) {
283  $links = explode(',', $matches[2][$i]);
284  foreach ($links as $link) {
285  if (preg_match('/^<([^>]+)>; *rel=([^ ]+)$/', trim($link), $match)) {
286  $rel = strtolower(utf8_decode($match[2]));
287  if ((strpos($rel, '"') === 0) || (strpos($rel, '?') === 0)) {
288  $rel = substr($rel, 1, strlen($rel) - 2);
289  }
290  if ($rel === 'previous') {
291  $rel = 'prev';
292  }
293  $this->relativeLinks[$rel] = $match[1];
294  }
295  }
296  }
297  }
298  }
299 }
const LOGLEVEL_NONE
No logging.
Definition: Util.php:141
static logDebug(string $message, bool $showSource=false)
Log a debug message.
Definition: Util.php:363
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
Class to represent an HTTP message request.
Definition: HttpMessage.php:30
static getHttpClient()
Get the HTTP client to use for sending the message.
parseRelativeLinks()
Parse the response headers for relative links.
array $relativeLinks
Relative links in response headers.
Definition: HttpMessage.php:75
$client
Class to implement the HTTP message interface using the Curl library.
Definition: CurlClient.php:28
getRelativeLinks()
Get the relative links.
static setHttpClient(ClientInterface $httpClient=null)
Set the HTTP client to use for sending the message.
static ClientInterface $httpClient
The client used to send the request.
int $status
Status of response (0 if undetermined).
Definition: HttpMessage.php:82
getMethod()
Get the HTTP method for the request.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
bool $ok
True if message was processed successfully.
Definition: HttpMessage.php:37
static logError(string $message, bool $showSource=true)
Log an error message.
Definition: Util.php:337
static int $logLevel
Current logging level.
Definition: Util.php:189
$message
Definition: xapiexit.php:32
Class to implement the HTTP message interface using a file stream.
static logInfo(string $message, bool $showSource=false)
Log an information message.
Definition: Util.php:350
hasRelativeLink(string $rel)
Check whether a relative link of the specified type exists.
Interface to represent an HTTP message client.
getUrl()
Get the target URL for the request.
__construct(string $url, string $method='GET', $params=null, string $header=null)
Class constructor.
$i
Definition: metadata.php:41
getRelativeLink(string $rel)
Get the URL from the relative link with the specified type.
send()
Send the request to the target URL.