ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
CurlRequest.php
Go to the documentation of this file.
1 <?php
2 
43 {
44 
52  public function setCurlOptions (array $options)
53  {
54  $this->_curlOptions = $options;
55  }
56  private $_curlOptions = array();
57 
63  protected function sendRequest ()
64  {
66 
67  /*********************************************************
68  * initialize the CURL session
69  *********************************************************/
70  $ch = $this->initAndConfigure();
71 
72  /*********************************************************
73  * Perform the query
74  *********************************************************/
75  $buf = curl_exec($ch);
76  if ( $buf === false ) {
77  phpCAS::trace('curl_exec() failed');
78  $this->storeErrorMessage(
79  'CURL error #'.curl_errno($ch).': '.curl_error($ch)
80  );
81  $res = false;
82  } else {
83  $this->storeResponseBody($buf);
84  phpCAS::trace("Response Body: \n".$buf."\n");
85  $res = true;
86 
87  }
88  // close the CURL session
89  curl_close($ch);
90 
92  return $res;
93  }
94 
102  public function initAndConfigure()
103  {
104  /*********************************************************
105  * initialize the CURL session
106  *********************************************************/
107  $ch = curl_init($this->url);
108 
109  if (version_compare(PHP_VERSION, '5.1.3', '>=')) {
110  //only avaible in php5
111  curl_setopt_array($ch, $this->_curlOptions);
112  } else {
113  foreach ($this->_curlOptions as $key => $value) {
114  curl_setopt($ch, $key, $value);
115  }
116  }
117 
118  /*********************************************************
119  * Set SSL configuration
120  *********************************************************/
121  if ($this->caCertPath) {
122  if ($this->validateCN) {
123  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
124  } else {
125  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
126  }
127  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
128  curl_setopt($ch, CURLOPT_CAINFO, $this->caCertPath);
129  phpCAS::trace('CURL: Set CURLOPT_CAINFO ' . $this->caCertPath);
130  } else {
131  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
132  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
133  }
134 
135  /*********************************************************
136  * Configure curl to capture our output.
137  *********************************************************/
138  // return the CURL output into a variable
139  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
140 
141  // get the HTTP header with a callback
142  curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_curlReadHeaders'));
143 
144  /*********************************************************
145  * Add cookie headers to our request.
146  *********************************************************/
147  if (count($this->cookies)) {
148  $cookieStrings = array();
149  foreach ($this->cookies as $name => $val) {
150  $cookieStrings[] = $name.'='.$val;
151  }
152  curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookieStrings));
153  }
154 
155  /*********************************************************
156  * Add any additional headers
157  *********************************************************/
158  if (count($this->headers)) {
159  curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
160  }
161 
162  /*********************************************************
163  * Flag and Body for POST requests
164  *********************************************************/
165  if ($this->isPost) {
166  curl_setopt($ch, CURLOPT_POST, 1);
167  curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody);
168  }
169 
170  return $ch;
171  }
172 
182  private function _storeResponseBody ($body)
183  {
184  $this->storeResponseBody($body);
185  }
186 
195  private function _curlReadHeaders ($ch, $header)
196  {
198  return strlen($header);
199  }
200 }
sendRequest()
Send the request and store the results.
Definition: CurlRequest.php:63
_storeResponseBody($body)
Store the response body.
This interface defines a class library for performing web requests.
setCurlOptions(array $options)
Set additional curl options.
Definition: CurlRequest.php:52
Provides support for performing web-requests via curl.
Definition: CurlRequest.php:40
static traceEnd($res='')
This method is used to indicate the end of the execution of a function in debug mode.
Definition: CAS.php:638
static trace($str)
This method is used to log something in debug mode.
Definition: CAS.php:579
_curlReadHeaders($ch, $header)
Internal method for capturing the headers from a curl request.
Provides support for performing web-requests via curl.
if(!is_array($argv)) $options
$header
storeErrorMessage($message)
Add a string to our error message.
initAndConfigure()
Internal method to initialize our cURL handle and configure the request.
Create styles array
The data for the language used.
storeResponseHeader($header)
Store a single response header to our array.
static traceBegin()
This method is used to indicate the start of the execution of a function in debug mode...
Definition: CAS.php:591
storeResponseBody($body)
Store the response body.