ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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, PHPMailer\PHPMailer\$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  }
$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, ilLogLevel\DEBUG, and ilLoggerFactory\getLogger().

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