ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
StreamClient.php
Go to the documentation of this file.
1 <?php
2 
20 
22 
30 class StreamClient implements ClientInterface
31 {
39 // public function send(HttpMessage $message)
40  public function send(\ILIAS\LTI\ToolProvider\Http\HttpMessage $message): bool
41  {
42  if (empty($message->requestHeaders)) {
43  $message->requestHeaders = ["Accept: */*"];
44  } elseif (count(preg_grep("/^Accept:/", $message->requestHeaders)) === 0) {
45  $message->requestHeaders[] = "Accept: */*";
46  }
47  $opts = [
48  'method' => $message->getMethod(),
49  'content' => $message->request,
50  'header' => $message->requestHeaders,
51  'ignore_errors' => true,
52  ];
53 
54  $message->requestHeaders = implode("\n", $message->requestHeaders);
55  try {
56  $ctx = stream_context_create(['http' => $opts]);
57  $fp = @fopen($message->getUrl(), 'rb', false, $ctx);
58  if ($fp) {
59  $resp = @stream_get_contents($fp);
60  $message->ok = $resp !== false;
61  if ($message->ok) {
62  $message->response = $resp;
63  // see http://php.net/manual/en/reserved.variables.httpresponseheader.php
64  if (isset($http_response_header[0])) {
65  $message->responseHeaders = trim(implode("\n", $http_response_header));
66  if (preg_match("/HTTP\/\d.\d\s+(\d+)/", $http_response_header[0], $out)) {
67  $message->status = $out[1];
68  }
69  $message->ok = $message->status < 400;
70  if (!$message->ok) {
71  $message->error = $http_response_header[0];
72  }
73  }
74  return $message->ok;
75  }
76  }
77  } catch (\Exception $e) {
78  $message->error = $e->getMessage();
79  $message->ok = false;
80  return false;
81  }
82  $message->error = error_get_last()["message"];
83  $message->ok = false;
84  return false;
85  }
86 }
Class ChatMainBarProvider .
Class to represent an HTTP message request.
Definition: HttpMessage.php:30
send(\ILIAS\LTI\ToolProvider\Http\HttpMessage $message)
Send the request to the target URL.
$out
Definition: buildRTE.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$message
Definition: xapiexit.php:32
Class to implement the HTTP message interface using a file stream.
Interface to represent an HTTP message client.