ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
CurlRequest.php
Go to the documentation of this file.
1<?php
2
41{
42
50 public function setCurlOptions(array $options)
51 {
52 $this->_curlOptions = $options;
53 }
54 private $_curlOptions = array();
55
61 protected function sendRequest()
62 {
64
65 /*********************************************************
66 * initialize the CURL session
67 *********************************************************/
68 $ch = $this->initAndConfigure();
69
70 /*********************************************************
71 * Perform the query
72 *********************************************************/
73 $buf = curl_exec($ch);
74 if ($buf === false) {
75 phpCAS::trace('curl_exec() failed');
76 $this->storeErrorMessage(
77 'CURL error #' . curl_errno($ch) . ': ' . curl_error($ch)
78 );
79 $res = false;
80 } else {
81 $this->storeResponseBody($buf);
82 phpCAS::trace("Response Body: \n" . $buf . "\n");
83 $res = true;
84 }
85 // close the CURL session
86 curl_close($ch);
87
89 return $res;
90 }
91
99 public function initAndConfigure()
100 {
101 /*********************************************************
102 * initialize the CURL session
103 *********************************************************/
104 $ch = curl_init($this->url);
105
106 if (version_compare(PHP_VERSION, '5.1.3', '>=')) {
107 //only avaible in php5
108 curl_setopt_array($ch, $this->_curlOptions);
109 } else {
110 foreach ($this->_curlOptions as $key => $value) {
111 curl_setopt($ch, $key, $value);
112 }
113 }
114
115 /*********************************************************
116 * Set SSL configuration
117 *********************************************************/
118 if ($this->caCertPath) {
119 if ($this->validateCN) {
120 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
121 } else {
122 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
123 }
124 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
125 curl_setopt($ch, CURLOPT_CAINFO, $this->caCertPath);
126 phpCAS::trace('CURL: Set CURLOPT_CAINFO ' . $this->caCertPath);
127 } else {
128 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
129 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
130 }
131
132 /*********************************************************
133 * Configure curl to capture our output.
134 *********************************************************/
135 // return the CURL output into a variable
136 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
137
138 // get the HTTP header with a callback
139 curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_curlReadHeaders'));
140
141 /*********************************************************
142 * Add cookie headers to our request.
143 *********************************************************/
144 if (count($this->cookies)) {
145 $cookieStrings = array();
146 foreach ($this->cookies as $name => $val) {
147 $cookieStrings[] = $name . '=' . $val;
148 }
149 curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookieStrings));
150 }
151
152 /*********************************************************
153 * Add any additional headers
154 *********************************************************/
155 if (count($this->headers)) {
156 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
157 }
158
159 /*********************************************************
160 * Flag and Body for POST requests
161 *********************************************************/
162 if ($this->isPost) {
163 curl_setopt($ch, CURLOPT_POST, 1);
164 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody);
165 }
166
167 return $ch;
168 }
169
179 private function _storeResponseBody($body)
180 {
181 $this->storeResponseBody($body);
182 }
183
192 private function _curlReadHeaders($ch, $header)
193 {
194 $this->storeResponseHeader($header);
195 return strlen($header);
196 }
197}
An exception for terminatinating execution or to throw for unit testing.
Provides support for performing web-requests via curl.
storeErrorMessage($message)
Add a string to our error message.
storeResponseBody($body)
Store the response body.
storeResponseHeader($header)
Store a single response header to our array.
Provides support for performing web-requests via curl.
Definition: CurlRequest.php:41
_storeResponseBody($body)
Store the response body.
initAndConfigure()
Internal method to initialize our cURL handle and configure the request.
Definition: CurlRequest.php:99
setCurlOptions(array $options)
Set additional curl options.
Definition: CurlRequest.php:50
sendRequest()
Send the request and store the results.
Definition: CurlRequest.php:61
_curlReadHeaders($ch, $header)
Internal method for capturing the headers from a curl request.
static trace($str)
This method is used to log something in debug mode.
Definition: CAS.php:599
static traceEnd($res='')
This method is used to indicate the end of the execution of a function in debug mode.
Definition: CAS.php:658
static traceBegin()
This method is used to indicate the start of the execution of a function in debug mode.
Definition: CAS.php:611
This interface defines a class library for performing web requests.
if($format !==null) $name
Definition: metadata.php:230
foreach($_POST as $key=> $value) $res