ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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) {
110  throw new CAS_OutOfSequenceException(
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) {
129  throw new CAS_OutOfSequenceException(
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) {
148  throw new CAS_OutOfSequenceException(
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) {
165  throw new CAS_OutOfSequenceException(
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) {
184  throw new CAS_OutOfSequenceException(
185  'Request has already been sent cannot ' . __METHOD__
186  );
187  }
188  if (!$this->isPost) {
189  throw new CAS_OutOfSequenceException(
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) {
209  throw new CAS_OutOfSequenceException(
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) {
230  throw new CAS_OutOfSequenceException(
231  'Request has already been sent cannot send again.'
232  );
233  }
234  if (is_null($this->url) || !$this->url) {
235  throw new CAS_OutOfSequenceException(
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) {
311  throw new CAS_OutOfSequenceException(
312  'Request has not been sent yet. Cannot ' . __METHOD__
313  );
314  }
316  }
317 
324  public function getResponseStatusCode()
325  {
326  if (!$this->_sent) {
327  throw new CAS_OutOfSequenceException(
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) {
355  throw new CAS_OutOfSequenceException(
356  'Request has not been sent yet. Cannot ' . __METHOD__
357  );
358  }
359 
360  return $this->_responseBody;
361  }
362 
369  public function getErrorMessage()
370  {
371  if (!$this->_sent) {
372  throw new CAS_OutOfSequenceException(
373  'Request has not been sent yet. Cannot ' . __METHOD__
374  );
375  }
376  return $this->_errorMessage;
377  }
378 }
makePost()
Make the request a POST request rather than the default GET request.
addCookies(array $cookies)
Add an array of cookies to the request.
setSslCaCert($caCertPath, $validate_cn=true)
Specify the path to an SSL CA certificate to validate the server with.
addCookie($name, $value)
Add a cookie to the request.
getErrorMessage()
Answer a message describing any errors if the request failed.
setPostBody($body)
Add a POST body to the request.
This interface defines a class library for performing web requests.
setUrl($url)
Set the URL of the Request.
Provides support for performing web-requests via curl.
addHeader($header)
Add a header string to the request.
catch(Exception $e) $message
This class defines Exceptions that should be thrown when the sequence of operations is invalid...
getResponseStatusCode()
Answer HTTP status code of the response.
An Exception for problems performing requests.
Definition: Exception.php:40
storeErrorMessage($message)
Add a string to our error message.
getResponseBody()
Answer the body of response.
sendRequest()
Send the request and store the results.
addHeaders(array $headers)
Add an array of header strings to the request.
storeResponseHeaders(array $headers)
Store the response headers.
storeResponseHeader($header)
Store a single response header to our array.
storeResponseBody($body)
Store the response body.
send()
Perform the request.
getResponseHeaders()
Answer the headers of the response.