Go to the documentation of this file.00001 <?php
00002
00040 class CAS_Request_CurlRequest
00041 extends CAS_Request_AbstractRequest
00042 implements CAS_Request_RequestInterface
00043 {
00044
00052 public function setCurlOptions (array $options)
00053 {
00054 $this->_curlOptions = $options;
00055 }
00056 private $_curlOptions = array();
00057
00063 protected function sendRequest ()
00064 {
00065 phpCAS::traceBegin();
00066
00067
00068
00069
00070 $ch = $this->_initAndConfigure();
00071
00072
00073
00074
00075 $buf = curl_exec($ch);
00076 if ( $buf === false ) {
00077 phpCAS::trace('curl_exec() failed');
00078 $this->storeErrorMessage('CURL error #'.curl_errno($ch).': '.curl_error($ch));
00079 $res = false;
00080 } else {
00081 $this->storeResponseBody($buf);
00082 phpCAS::trace("Response Body: \n".$buf."\n");
00083 $res = true;
00084
00085 }
00086
00087 curl_close($ch);
00088
00089 phpCAS::traceEnd($res);
00090 return $res;
00091 }
00092
00100 private function _initAndConfigure()
00101 {
00102
00103
00104
00105 $ch = curl_init($this->url);
00106
00107 if (version_compare(PHP_VERSION, '5.1.3', '>=')) {
00108
00109 curl_setopt_array($ch, $this->_curlOptions);
00110 } else {
00111 foreach ($this->_curlOptions as $key => $value) {
00112 curl_setopt($ch, $key, $value);
00113 }
00114 }
00115
00116
00117
00118
00119 if ($this->caCertPath) {
00120 if ($this->validateCN) {
00121 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
00122 } else {
00123 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
00124 }
00125 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
00126 curl_setopt($ch, CURLOPT_CAINFO, $this->caCertPath);
00127 phpCAS::trace('CURL: Set CURLOPT_CAINFO ' . $this->caCertPath);
00128 } else {
00129 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
00130 }
00131
00132
00133
00134
00135
00136 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00137
00138
00139 curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_curlReadHeaders'));
00140
00141
00142
00143
00144 if (count($this->cookies)) {
00145 $cookieStrings = array();
00146 foreach ($this->cookies as $name => $val) {
00147 $cookieStrings[] = $name.'='.$val;
00148 }
00149 curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookieStrings));
00150 }
00151
00152
00153
00154
00155 if (count($this->headers)) {
00156 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
00157 }
00158
00159
00160
00161
00162 if ($this->isPost) {
00163 curl_setopt($ch, CURLOPT_POST, 1);
00164 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody);
00165 }
00166
00167 return $ch;
00168 }
00169
00179 private function _storeResponseBody ($body)
00180 {
00181 $this->storeResponseBody($body);
00182 }
00183
00192 private function _curlReadHeaders ($ch, $header)
00193 {
00194 $this->storeResponseHeader($header);
00195 return strlen($header);
00196 }
00197 }