ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
105 final public function init()
106 {
107 // teminate existing handles
108 $this->close();
109 if (strlen($this->url)) {
110 $this->ch = curl_init($this->url);
111 #$GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': ' . $this->url);
112 } else {
113 $this->ch = curl_init();
114 }
115 if (!$this->ch) {
116 throw new ilCurlConnectionException('Cannot init curl connection.');
117 }
118 if (curl_errno($this->ch)) {
119 throw new ilCurlConnectionException(curl_error($this->ch), curl_errno($this->ch));
120 }
121
122 return true;
123 }
124
134 final public function setOpt($a_option, $a_value)
135 {
136 if (!@curl_setopt($this->ch, $a_option, $a_value)) {
137 throw new ilCurlConnectionException('Invalid option given for: ' . $a_option, curl_errno($this->ch));
138 }
139 return true;
140 }
141
149 final public function exec()
150 {
151 // Add header function
152 curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this,'parseHeader'));
153 if ((@$res = curl_exec($this->ch)) === false) {
154 if (strlen($err = curl_error($this->ch))) {
155 throw new ilCurlConnectionException($err, curl_errno($this->ch));
156 } else {
157 throw new ilCurlConnectionException('Error calling curl_exec().');
158 }
159 }
160 return $res;
161 }
162
167 public function parseResponse($a_response)
168 {
169 $header_size = $this->getInfo(CURLINFO_HEADER_SIZE);
170
171 $this->header_plain = substr($a_response, 0, $header_size);
172 $this->response_body = substr($a_response, $header_size);
173 }
174
179 public function getResponseBody()
180 {
182 }
183
192 public function getInfo($opt = 0)
193 {
194 if ($opt) {
195 $res = curl_getinfo($this->ch, $opt);
196 } else {
197 $res = curl_getinfo($this->ch);
198 }
199 return $res;
200 }
201
208 private function parseHeader($handle, $header)
209 {
210 $this->header_plain = $header;
211
212 $lines = explode('\r\n', $this->getResponseHeader());
213 foreach ($lines as $line) {
214 list($name, $value) = explode(':', $line, 2);
215 $this->header_arr[$name] = $value;
216 }
217 return strlen($this->getResponseHeader());
218 }
219
226 final public function close()
227 {
228 if ($this->ch != null) {
229 curl_close($this->ch);
230 $this->ch = null;
231 }
232 }
233
239 public function __destruct()
240 {
241 $this->close();
242 }
243}
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
__construct($a_url='')
Constructor.
getResponseBody()
Get responce body.
close()
Close connection.
parseHeader($handle, $header)
Parse respone header.
init()
Init curl connection.
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.
foreach($_POST as $key=> $value) $res