ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 if (strlen($this->url)) {
108 $this->ch = curl_init($this->url);
109 #$GLOBALS['ilLog']->write(__METHOD__ . ': ' . $this->url);
110 } else {
111 $this->ch = curl_init();
112 }
113 if (!$this->ch) {
114 throw new ilCurlConnectionException('Cannot init curl connection.');
115 }
116 if (curl_errno($this->ch)) {
117 throw new ilCurlConnectionException(curl_error($this->ch), curl_errno($this->ch));
118 }
119
120 return true;
121 }
122
132 final public function setOpt($a_option, $a_value)
133 {
134 if (!@curl_setopt($this->ch, $a_option, $a_value)) {
135 throw new ilCurlConnectionException('Invalid option given for: ' . $a_option, curl_errno($this->ch));
136 }
137 return true;
138 }
139
147 final public function exec()
148 {
149 // Add header function
150 curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this,'parseHeader'));
151 if ((@$res = curl_exec($this->ch)) === false) {
152 if (strlen($err = curl_error($this->ch))) {
153 throw new ilCurlConnectionException($err, curl_errno($this->ch));
154 } else {
155 throw new ilCurlConnectionException('Error calling curl_exec().');
156 }
157 }
158 return $res;
159 }
160
165 public function parseResponse($a_response)
166 {
167 $header_size = $this->getInfo(CURLINFO_HEADER_SIZE);
168
169 $this->header_plain = substr($a_response, 0, $header_size);
170 $this->response_body = substr($a_response, $header_size);
171 }
172
177 public function getResponseBody()
178 {
180 }
181
190 public function getInfo($opt = 0)
191 {
192 if ($opt) {
193 $res = curl_getinfo($this->ch, $opt);
194 } else {
195 $res = curl_getinfo($this->ch);
196 }
197 return $res;
198 }
199
206 private function parseHeader($handle, $header)
207 {
208 $this->header_plain = $header;
209
210 $lines = explode('\r\n', $this->getResponseHeader());
211 foreach ($lines as $line) {
212 list($name, $value) = explode(':', $line, 2);
213 $this->header_arr[$name] = $value;
214 }
215 return strlen($this->getResponseHeader());
216 }
217
224 final public function close()
225 {
226 if ($this->ch != null) {
227 curl_close($this->ch);
228 $this->ch = null;
229 }
230 }
231
237 public function __destruct()
238 {
239 $this->close();
240 }
241}
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.
if($format !==null) $name
Definition: metadata.php:146
foreach($_POST as $key=> $value) $res