phpCAS version 1.3.6

CurlRequest.php
Go to the documentation of this file.
1 <?php
2
40 class CAS_Request_CurlRequest
41 extends CAS_Request_AbstractRequest
42 implements CAS_Request_RequestInterface
43 {
44
52 public function setCurlOptions (array $options)
53 {
54 $this->_curlOptions = $options;
55 }
56 private $_curlOptions = array();
57
63 protected function sendRequest ()
64 {
65 phpCAS::traceBegin();
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
91 phpCAS::traceEnd($res);
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 {
197 $this->storeResponseHeader($header);
198 return strlen($header);
199 }
200 }
CAS_Request_CurlRequest\sendRequest
sendRequest()
Definition: CurlRequest.php:63
CAS_Request_CurlRequest\_storeResponseBody
_storeResponseBody($body)
Definition: CurlRequest.php:182
CAS_Request_RequestInterface
Definition: RequestInterface.php:40
CAS_Request_CurlRequest\setCurlOptions
setCurlOptions(array $options)
Definition: CurlRequest.php:52
CAS_Request_CurlRequest
Definition: CurlRequest.php:40
phpCAS\traceEnd
static traceEnd($res='')
Definition: CAS.php:638
phpCAS\trace
static trace($str)
Definition: CAS.php:579
CAS_Request_CurlRequest\_curlReadHeaders
_curlReadHeaders($ch, $header)
Definition: CurlRequest.php:195
CAS_Request_AbstractRequest
Definition: AbstractRequest.php:40
CAS_Request_AbstractRequest\storeErrorMessage
storeErrorMessage($message)
Definition: AbstractRequest.php:295
CAS_Request_CurlRequest\$_curlOptions
$_curlOptions
Definition: CurlRequest.php:56
CAS_Request_CurlRequest\initAndConfigure
initAndConfigure()
Definition: CurlRequest.php:102
CAS_Request_AbstractRequest\storeResponseHeader
storeResponseHeader($header)
Definition: AbstractRequest.php:271
phpCAS\traceBegin
static traceBegin()
Definition: CAS.php:591
CAS_Request_AbstractRequest\storeResponseBody
storeResponseBody($body)
Definition: AbstractRequest.php:283