ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
IMSGlobal\LTI\HTTPMessage Class Reference

Class to represent an HTTP message. More...

+ Collaboration diagram for IMSGlobal\LTI\HTTPMessage:

Public Member Functions

 __construct ($url, $method='GET', $params=null, $header=null)
 Class constructor. More...
 
 send ()
 Send the request to the target URL. More...
 

Data Fields

 $ok = false
 True if message was sent successfully. More...
 
 $request = null
 Request body. More...
 
 $requestHeaders = ''
 Request headers. More...
 
 $response = null
 Response body. More...
 
 $responseHeaders = ''
 Response headers. More...
 
 $status = 0
 Status of response (0 if undetermined). More...
 
 $error = ''
 Error message. More...
 

Private Attributes

 $url = null
 Request URL. More...
 
 $method = null
 Request method. More...
 

Detailed Description

Class to represent an HTTP message.

Author
Stephen P Vickers svick.nosp@m.ers@.nosp@m.imsgl.nosp@m.obal.nosp@m..org
Date
2016
Version
3.0.0 http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0

Definition at line 14 of file HTTPMessage.php.

Constructor & Destructor Documentation

◆ __construct()

IMSGlobal\LTI\HTTPMessage::__construct (   $url,
  $method = 'GET',
  $params = null,
  $header = null 
)

Class constructor.

Parameters
string$urlURL to send request to
string$methodRequest method to use (optional, default is GET)
mixed$paramsAssociative array of parameter values to be passed or message body (optional, default is none)
string$headerValues to include in the request header (optional, default is none)

Definition at line 88 of file HTTPMessage.php.

References $header, IMSGlobal\LTI\HTTPMessage\$method, $params, and IMSGlobal\LTI\HTTPMessage\$url.

89  {
90 
91  $this->url = $url;
92  $this->method = strtoupper($method);
93  if (is_array($params)) {
94  $this->request = http_build_query($params);
95  } else {
96  $this->request = $params;
97  }
98  if (!empty($header)) {
99  $this->requestHeaders = explode("\n", $header);
100  }
101 
102  }
$params
Definition: disable.php:11
$method
Request method.
Definition: HTTPMessage.php:78

Member Function Documentation

◆ send()

IMSGlobal\LTI\HTTPMessage::send ( )

Send the request to the target URL.

Returns
boolean True if the request was successful

Definition at line 109 of file HTTPMessage.php.

References IMSGlobal\LTI\HTTPMessage\$ok, IMSGlobal\LTI\HTTPMessage\$requestHeaders, array, ilLogLevel\DEBUG, and ilLoggerFactory\getLogger().

110  {
111 
112  $this->ok = false;
113 // Try using curl if available
114  if (function_exists('curl_init')) {
115  $resp = '';
116  $ch = curl_init();
117  curl_setopt($ch, CURLOPT_URL, $this->url);
118  if (!empty($this->requestHeaders)) {
119  curl_setopt($ch, CURLOPT_HTTPHEADER, $this->requestHeaders);
120  } else {
121  curl_setopt($ch, CURLOPT_HEADER, 0);
122  }
123  if ($this->method === 'POST') {
124  curl_setopt($ch, CURLOPT_POST, true);
125  curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
126  } else if ($this->method !== 'GET') {
127  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
128  if (!is_null($this->request)) {
129  curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
130  }
131  }
132  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
133  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
134  curl_setopt($ch, CURLOPT_HEADER, true);
135  // begin-patch ilias
136  #curl_setopt($ch, CURLOPT_SSLVERSION,3);
137  $chResp = curl_exec($ch);
138  \ilLoggerFactory::getLogger('lti')->dump(curl_getinfo($ch), \ilLogLevel::DEBUG);
139  \ilLoggerFactory::getLogger('lti')->dump(curl_error($ch), \ilLogLevel::DEBUG);
140 
141  $this->ok = $chResp !== false;
142  if ($this->ok) {
143  $chResp = str_replace("\r\n", "\n", $chResp);
144  $chRespSplit = explode("\n\n", $chResp, 2);
145  if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) === 'HTTP/')) {
146  $chRespSplit = explode("\n\n", $chRespSplit[1], 2);
147  }
148  $this->responseHeaders = $chRespSplit[0];
149  $resp = $chRespSplit[1];
150  $this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
151  $this->ok = $this->status < 400;
152  if (!$this->ok) {
153  $this->error = curl_error($ch);
154  }
155  }
156  $this->requestHeaders = str_replace("\r\n", "\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
157  curl_close($ch);
158  $this->response = $resp;
159  } else {
160 // Try using fopen if curl was not available
161  $opts = array('method' => $this->method,
162  'content' => $this->request
163  );
164  if (!empty($this->requestHeaders)) {
165  $opts['header'] = $this->requestHeaders;
166  }
167  try {
168  $ctx = stream_context_create(array('http' => $opts));
169  $fp = @fopen($this->url, 'rb', false, $ctx);
170  if ($fp) {
171  $resp = @stream_get_contents($fp);
172  $this->ok = $resp !== false;
173  }
174  } catch (\Exception $e) {
175  $this->ok = false;
176  }
177  }
178 
179  return $this->ok;
180 
181  }
$ok
True if message was sent successfully.
Definition: HTTPMessage.php:22
$requestHeaders
Request headers.
Definition: HTTPMessage.php:36
Create styles array
The data for the language used.
static getLogger($a_component_id)
Get component logger.
+ Here is the call graph for this function:

Field Documentation

◆ $error

error IMSGlobal\LTI\HTTPMessage::$error = ''

Error message.

Definition at line 64 of file HTTPMessage.php.

◆ $method

method IMSGlobal\LTI\HTTPMessage::$method = null
private

Request method.

Definition at line 78 of file HTTPMessage.php.

Referenced by IMSGlobal\LTI\HTTPMessage\__construct().

◆ $ok

boolean IMSGlobal\LTI\HTTPMessage::$ok = false

True if message was sent successfully.

Definition at line 22 of file HTTPMessage.php.

Referenced by IMSGlobal\LTI\HTTPMessage\send().

◆ $request

request IMSGlobal\LTI\HTTPMessage::$request = null

Request body.

Definition at line 29 of file HTTPMessage.php.

◆ $requestHeaders

request_headers IMSGlobal\LTI\HTTPMessage::$requestHeaders = ''

Request headers.

Definition at line 36 of file HTTPMessage.php.

Referenced by IMSGlobal\LTI\HTTPMessage\send().

◆ $response

response IMSGlobal\LTI\HTTPMessage::$response = null

Response body.

Definition at line 43 of file HTTPMessage.php.

◆ $responseHeaders

response_headers IMSGlobal\LTI\HTTPMessage::$responseHeaders = ''

Response headers.

Definition at line 50 of file HTTPMessage.php.

◆ $status

status IMSGlobal\LTI\HTTPMessage::$status = 0

Status of response (0 if undetermined).

Definition at line 57 of file HTTPMessage.php.

◆ $url

url IMSGlobal\LTI\HTTPMessage::$url = null
private

Request URL.

Definition at line 71 of file HTTPMessage.php.

Referenced by IMSGlobal\LTI\HTTPMessage\__construct().


The documentation for this class was generated from the following file: