ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
AbstractRequest.php
Go to the documentation of this file.
1 <?php
2 
42 {
43 
44  protected $url = null;
45  protected $cookies = array();
46  protected $headers = array();
47  protected $isPost = false;
48  protected $postBody = null;
49  protected $caCertPath = null;
50  protected $validateCN = true;
51  private $_sent = false;
52  private $_responseHeaders = array();
53  private $_responseBody = null;
54  private $_errorMessage = '';
55 
56  /*********************************************************
57  * Configure the Request
58  *********************************************************/
59 
68  public function setUrl ($url)
69  {
70  if ($this->_sent) {
72  'Request has already been sent cannot '.__METHOD__
73  );
74  }
75 
76  $this->url = $url;
77  }
78 
88  public function addCookie ($name, $value)
89  {
90  if ($this->_sent) {
92  'Request has already been sent cannot '.__METHOD__
93  );
94  }
95 
96  $this->cookies[$name] = $value;
97  }
98 
109  public function addCookies (array $cookies)
110  {
111  if ($this->_sent) {
112  throw new CAS_OutOfSequenceException(
113  'Request has already been sent cannot '.__METHOD__
114  );
115  }
116 
117  $this->cookies = array_merge($this->cookies, $cookies);
118  }
119 
128  public function addHeader ($header)
129  {
130  if ($this->_sent) {
131  throw new CAS_OutOfSequenceException(
132  'Request has already been sent cannot '.__METHOD__
133  );
134  }
135 
136  $this->headers[] = $header;
137  }
138 
147  public function addHeaders (array $headers)
148  {
149  if ($this->_sent) {
150  throw new CAS_OutOfSequenceException(
151  'Request has already been sent cannot '.__METHOD__
152  );
153  }
154 
155  $this->headers = array_merge($this->headers, $headers);
156  }
157 
164  public function makePost ()
165  {
166  if ($this->_sent) {
167  throw new CAS_OutOfSequenceException(
168  'Request has already been sent cannot '.__METHOD__
169  );
170  }
171 
172  $this->isPost = true;
173  }
174 
183  public function setPostBody ($body)
184  {
185  if ($this->_sent) {
186  throw new CAS_OutOfSequenceException(
187  'Request has already been sent cannot '.__METHOD__
188  );
189  }
190  if (!$this->isPost) {
191  throw new CAS_OutOfSequenceException(
192  'Cannot add a POST body to a GET request, use makePost() first.'
193  );
194  }
195 
196  $this->postBody = $body;
197  }
198 
208  public function setSslCaCert ($caCertPath,$validate_cn=true)
209  {
210  if ($this->_sent) {
211  throw new CAS_OutOfSequenceException(
212  'Request has already been sent cannot '.__METHOD__
213  );
214  }
215  $this->caCertPath = $caCertPath;
216  $this->validateCN = $validate_cn;
217  }
218 
219  /*********************************************************
220  * 2. Send the Request
221  *********************************************************/
222 
229  public function send ()
230  {
231  if ($this->_sent) {
232  throw new CAS_OutOfSequenceException(
233  'Request has already been sent cannot send again.'
234  );
235  }
236  if (is_null($this->url) || !$this->url) {
237  throw new CAS_OutOfSequenceException(
238  'A url must be specified via setUrl() before the request can be sent.'
239  );
240  }
241  $this->_sent = true;
242  return $this->sendRequest();
243  }
244 
250  abstract protected function sendRequest ();
251 
259  protected function storeResponseHeaders (array $headers)
260  {
261  $this->_responseHeaders = array_merge($this->_responseHeaders, $headers);
262  }
263 
271  protected function storeResponseHeader ($header)
272  {
273  $this->_responseHeaders[] = $header;
274  }
275 
283  protected function storeResponseBody ($body)
284  {
285  $this->_responseBody = $body;
286  }
287 
295  protected function storeErrorMessage ($message)
296  {
297  $this->_errorMessage .= $message;
298  }
299 
300  /*********************************************************
301  * 3. Access the response
302  *********************************************************/
303 
310  public function getResponseHeaders ()
311  {
312  if (!$this->_sent) {
313  throw new CAS_OutOfSequenceException(
314  'Request has not been sent yet. Cannot '.__METHOD__
315  );
316  }
318  }
319 
326  public function getResponseStatusCode ()
327  {
328  if (!$this->_sent) {
329  throw new CAS_OutOfSequenceException(
330  'Request has not been sent yet. Cannot '.__METHOD__
331  );
332  }
333 
334  if (!preg_match(
335  '/HTTP\/[0-9.]+\s+([0-9]+)\s*(.*)/',
336  $this->_responseHeaders[0], $matches
337  )
338  ) {
339  throw new CAS_Request_Exception(
340  'Bad response, no status code was found in the first line.'
341  );
342  }
343 
344  return intval($matches[1]);
345  }
346 
353  public function getResponseBody ()
354  {
355  if (!$this->_sent) {
356  throw new CAS_OutOfSequenceException(
357  'Request has not been sent yet. Cannot '.__METHOD__
358  );
359  }
360 
361  return $this->_responseBody;
362  }
363 
370  public function getErrorMessage ()
371  {
372  if (!$this->_sent) {
373  throw new CAS_OutOfSequenceException(
374  'Request has not been sent yet. Cannot '.__METHOD__
375  );
376  }
377  return $this->_errorMessage;
378  }
379 }
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.
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
$header
storeErrorMessage($message)
Add a string to our error message.
getResponseBody()
Answer the body of response.
sendRequest()
Send the request and store the results.
Create styles array
The data for the language used.
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.