ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCurlConnection.php
Go to the documentation of this file.
1<?php
2
3/*
4 +-----------------------------------------------------------------------------+
5 | ILIAS open source |
6 +-----------------------------------------------------------------------------+
7 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
8 | |
9 | This program is free software; you can redistribute it and/or |
10 | modify it under the terms of the GNU General Public License |
11 | as published by the Free Software Foundation; either version 2 |
12 | of the License, or (at your option) any later version. |
13 | |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | GNU General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software |
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22 +-----------------------------------------------------------------------------+
23 */
24
36include_once('Services/WebServices/Curl/classes/class.ilCurlConnectionException.php');
37
39{
40 protected $url = '';
41 protected $ch = null;
42
43 private $header_plain = '';
44 private $header_arr = array();
45
46 private $response_body = '';
47
56 public function __construct($a_url = '')
57 {
58 $this->url = $a_url;
59
60 if (!self::_isCurlExtensionLoaded()) {
61 throw new ilCurlConnectionException('Curl extension not enabled.');
62 }
63 }
64
72 final public static function _isCurlExtensionLoaded()
73 {
74 if (!function_exists('curl_init')) {
75 return false;
76 }
77 return true;
78 }
79
84 public function getResponseHeader()
85 {
87 }
88
93 public function getResponseHeaderArray()
94 {
95 return (array) $this->header_arr;
96 }
97
107 final public function init(bool $set_proxy = true)
108 {
109 // teminate existing handles
110 $this->close();
111 if (strlen($this->url)) {
112 $this->ch = curl_init($this->url);
113 #$GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': ' . $this->url);
114 } else {
115 $this->ch = curl_init();
116 }
117 if (!$this->ch) {
118 throw new ilCurlConnectionException('Cannot init curl connection.');
119 }
120 if (curl_errno($this->ch)) {
121 throw new ilCurlConnectionException(curl_error($this->ch), curl_errno($this->ch));
122 }
123
124 if ($set_proxy) {
125 // use a proxy, if configured by ILIAS
127 if ($proxy->isActive()) {
128 $this->setOpt(CURLOPT_HTTPPROXYTUNNEL, true);
129
130 if (!empty($proxy->getHost())) {
131 $this->setOpt(CURLOPT_PROXY, $proxy->getHost());
132 } else {
133 throw new ilCurlConnectionException("Proxy host is not set on activated proxy");
134 }
135
136 if (!empty($proxy->getPort())) {
137 $this->setOpt(CURLOPT_PROXYPORT, $proxy->getPort());
138 }
139 }
140 }
141
142 return true;
143 }
144
154 final public function setOpt($a_option, $a_value)
155 {
156 if (!@curl_setopt($this->ch, $a_option, $a_value)) {
157 throw new ilCurlConnectionException('Invalid option given for: ' . $a_option, curl_errno($this->ch));
158 }
159 return true;
160 }
161
169 final public function exec()
170 {
171 // Add header function
172 curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this,'parseHeader'));
173 if ((@$res = curl_exec($this->ch)) === false) {
174 if (strlen($err = curl_error($this->ch))) {
175 throw new ilCurlConnectionException($err, curl_errno($this->ch));
176 } else {
177 throw new ilCurlConnectionException('Error calling curl_exec().');
178 }
179 }
180 return $res;
181 }
182
187 public function parseResponse($a_response)
188 {
189 $header_size = $this->getInfo(CURLINFO_HEADER_SIZE);
190
191 $this->header_plain = substr($a_response, 0, $header_size);
192 $this->response_body = substr($a_response, $header_size);
193 }
194
199 public function getResponseBody()
200 {
202 }
203
212 public function getInfo($opt = 0)
213 {
214 if ($opt) {
215 $res = curl_getinfo($this->ch, $opt);
216 } else {
217 $res = curl_getinfo($this->ch);
218 }
219 return $res;
220 }
221
228 private function parseHeader($handle, $header)
229 {
230 $this->header_plain = $header;
231
232 $lines = explode('\r\n', $this->getResponseHeader());
233 foreach ($lines as $line) {
234 list($name, $value) = explode(':', $line, 2);
235 $this->header_arr[$name] = $value;
236 }
237 return strlen($this->getResponseHeader());
238 }
239
246 final public function close()
247 {
248 if ($this->ch != null) {
249 curl_close($this->ch);
250 $this->ch = null;
251 }
252 }
253
259 public function __destruct()
260 {
261 $this->close();
262 }
263}
An exception for terminatinating execution or to throw for unit testing.
getInfo($opt=0)
Get informations about a specific transfer.
parseResponse($a_response)
parse response body
init(bool $set_proxy=true)
Init curl connection.
__construct($a_url='')
Constructor.
getResponseBody()
Get responce body.
close()
Close connection.
parseHeader($handle, $header)
Parse respone header.
setOpt($a_option, $a_value)
Wrapper for curl_setopt.
exec()
Wrapper for curl_exec.
static _isCurlExtensionLoaded()
Check if curl extension is loaded.
getResponseHeaderArray()
Get response header as array.
__destruct()
Destructor @access public.
getResponseHeader()
Get response header as string.
static _getInstance()
Getter for unique instance.
if($format !==null) $name
Definition: metadata.php:230
foreach($_POST as $key=> $value) $res