ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
HTTPMessage.php
Go to the documentation of this file.
1<?php
2
3namespace IMSGlobal\LTI;
4
15{
16
22 public $ok = false;
23
29 public $request = null;
30
36 public $requestHeaders = '';
37
43 public $response = null;
44
50 public $responseHeaders = '';
51
57 public $status = 0;
58
64 public $error = '';
65
71 private $url = null;
72
78 private $method = null;
79
88 function __construct($url, $method = 'GET', $params = null, $header = null)
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 }
103
109 public function send()
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 }
186
187}
An exception for terminatinating execution or to throw for unit testing.
Class to represent an HTTP message.
Definition: HTTPMessage.php:15
$responseHeaders
Response headers.
Definition: HTTPMessage.php:50
$method
Request method.
Definition: HTTPMessage.php:78
__construct($url, $method='GET', $params=null, $header=null)
Class constructor.
Definition: HTTPMessage.php:88
$status
Status of response (0 if undetermined).
Definition: HTTPMessage.php:57
$ok
True if message was sent successfully.
Definition: HTTPMessage.php:22
send()
Send the request to the target URL.
$requestHeaders
Request headers.
Definition: HTTPMessage.php:36
error($a_errmsg)
set error message @access public
static getLogger($a_component_id)
Get component logger.