ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $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 }
182
183}
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.
$params
Definition: disable.php:11