ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
AbstractRequest.php
Go to the documentation of this file.
1<?php
2
41{
42 protected $url = null;
43 protected $cookies = array();
44 protected $headers = array();
45 protected $isPost = false;
46 protected $postBody = null;
47 protected $caCertPath = null;
48 protected $validateCN = true;
49 private $_sent = false;
50 private $_responseHeaders = array();
51 private $_responseBody = null;
52 private $_errorMessage = '';
53
54 /*********************************************************
55 * Configure the Request
56 *********************************************************/
57
66 public function setUrl($url)
67 {
68 if ($this->_sent) {
70 'Request has already been sent cannot ' . __METHOD__
71 );
72 }
73
74 $this->url = $url;
75 }
76
86 public function addCookie($name, $value)
87 {
88 if ($this->_sent) {
90 'Request has already been sent cannot ' . __METHOD__
91 );
92 }
93
94 $this->cookies[$name] = $value;
95 }
96
107 public function addCookies(array $cookies)
108 {
109 if ($this->_sent) {
111 'Request has already been sent cannot ' . __METHOD__
112 );
113 }
114
115 $this->cookies = array_merge($this->cookies, $cookies);
116 }
117
126 public function addHeader($header)
127 {
128 if ($this->_sent) {
130 'Request has already been sent cannot ' . __METHOD__
131 );
132 }
133
134 $this->headers[] = $header;
135 }
136
145 public function addHeaders(array $headers)
146 {
147 if ($this->_sent) {
149 'Request has already been sent cannot ' . __METHOD__
150 );
151 }
152
153 $this->headers = array_merge($this->headers, $headers);
154 }
155
162 public function makePost()
163 {
164 if ($this->_sent) {
166 'Request has already been sent cannot ' . __METHOD__
167 );
168 }
169
170 $this->isPost = true;
171 }
172
181 public function setPostBody($body)
182 {
183 if ($this->_sent) {
185 'Request has already been sent cannot ' . __METHOD__
186 );
187 }
188 if (!$this->isPost) {
190 'Cannot add a POST body to a GET request, use makePost() first.'
191 );
192 }
193
194 $this->postBody = $body;
195 }
196
206 public function setSslCaCert($caCertPath, $validate_cn = true)
207 {
208 if ($this->_sent) {
210 'Request has already been sent cannot ' . __METHOD__
211 );
212 }
213 $this->caCertPath = $caCertPath;
214 $this->validateCN = $validate_cn;
215 }
216
217 /*********************************************************
218 * 2. Send the Request
219 *********************************************************/
220
227 public function send()
228 {
229 if ($this->_sent) {
231 'Request has already been sent cannot send again.'
232 );
233 }
234 if (is_null($this->url) || !$this->url) {
236 'A url must be specified via setUrl() before the request can be sent.'
237 );
238 }
239 $this->_sent = true;
240 return $this->sendRequest();
241 }
242
248 abstract protected function sendRequest();
249
257 protected function storeResponseHeaders(array $headers)
258 {
259 $this->_responseHeaders = array_merge($this->_responseHeaders, $headers);
260 }
261
269 protected function storeResponseHeader($header)
270 {
271 $this->_responseHeaders[] = $header;
272 }
273
281 protected function storeResponseBody($body)
282 {
283 $this->_responseBody = $body;
284 }
285
293 protected function storeErrorMessage($message)
294 {
295 $this->_errorMessage .= $message;
296 }
297
298 /*********************************************************
299 * 3. Access the response
300 *********************************************************/
301
308 public function getResponseHeaders()
309 {
310 if (!$this->_sent) {
312 'Request has not been sent yet. Cannot ' . __METHOD__
313 );
314 }
316 }
317
324 public function getResponseStatusCode()
325 {
326 if (!$this->_sent) {
328 'Request has not been sent yet. Cannot ' . __METHOD__
329 );
330 }
331
332 if (!preg_match(
333 '/HTTP\/[0-9.]+\s+([0-9]+)\s*(.*)/',
334 $this->_responseHeaders[0],
335 $matches
336 )
337 ) {
338 throw new CAS_Request_Exception(
339 'Bad response, no status code was found in the first line.'
340 );
341 }
342
343 return intval($matches[1]);
344 }
345
352 public function getResponseBody()
353 {
354 if (!$this->_sent) {
356 'Request has not been sent yet. Cannot ' . __METHOD__
357 );
358 }
359
361 }
362
369 public function getErrorMessage()
370 {
371 if (!$this->_sent) {
373 'Request has not been sent yet. Cannot ' . __METHOD__
374 );
375 }
377 }
378}
An exception for terminatinating execution or to throw for unit testing.
This class defines Exceptions that should be thrown when the sequence of operations is invalid.
Provides support for performing web-requests via curl.
sendRequest()
Send the request and store the results.
getResponseBody()
Answer the body of response.
storeErrorMessage($message)
Add a string to our error message.
storeResponseBody($body)
Store the response body.
getResponseStatusCode()
Answer HTTP status code of the response.
getErrorMessage()
Answer a message describing any errors if the request failed.
addCookies(array $cookies)
Add an array of cookies to the request.
setUrl($url)
Set the URL of the Request.
addHeaders(array $headers)
Add an array of header strings to the request.
addHeader($header)
Add a header string to the request.
setPostBody($body)
Add a POST body to the request.
setSslCaCert($caCertPath, $validate_cn=true)
Specify the path to an SSL CA certificate to validate the server with.
send()
Perform the request.
makePost()
Make the request a POST request rather than the default GET request.
storeResponseHeaders(array $headers)
Store the response headers.
storeResponseHeader($header)
Store a single response header to our array.
addCookie($name, $value)
Add a cookie to the request.
getResponseHeaders()
Answer the headers of the response.
An Exception for problems performing requests.
Definition: Exception.php:41
This interface defines a class library for performing web requests.
if($format !==null) $name
Definition: metadata.php:230
$message
Definition: xapiexit.php:14