ILIAS  release_8 Revision v8.24
class.ilCurlConnection.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/*
6 +-----------------------------------------------------------------------------+
7 | ILIAS open source |
8 +-----------------------------------------------------------------------------+
9 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
10 | |
11 | This program is free software; you can redistribute it and/or |
12 | modify it under the terms of the GNU General Public License |
13 | as published by the Free Software Foundation; either version 2 |
14 | of the License, or (at your option) any later version. |
15 | |
16 | This program is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | GNU General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU General Public License |
22 | along with this program; if not, write to the Free Software |
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
24 +-----------------------------------------------------------------------------+
25 */
26
36{
37 protected string $url = '';
38
40 protected $ch = null;
41 private string $header_plain = '';
42 private array $header_arr = array();
43 private string $response_body = '';
44
45 public function __construct(string $a_url = '')
46 {
47 $this->url = $a_url;
48
49 if (!self::_isCurlExtensionLoaded()) {
50 throw new ilCurlConnectionException('Curl extension not enabled.');
51 }
52 }
53
54 final public static function _isCurlExtensionLoaded(): bool
55 {
56 if (!function_exists('curl_init')) {
57 return false;
58 }
59 return true;
60 }
61
62 public function getResponseHeader(): string
63 {
65 }
66
67 public function getResponseHeaderArray(): array
68 {
69 return $this->header_arr;
70 }
71
72 final public function init(bool $set_proxy = true): bool
73 {
74 // teminate existing handles
75 $this->close();
76 if ($this->url !== '') {
77 $this->ch = curl_init($this->url);
78 } else {
79 $this->ch = curl_init();
80 }
81 if (!$this->ch) {
82 throw new ilCurlConnectionException('Cannot init curl connection.');
83 }
84 if (curl_errno($this->ch)) {
85 throw new ilCurlConnectionException(curl_error($this->ch), curl_errno($this->ch));
86 }
87
88 if ($set_proxy) {
89 // use a proxy, if configured by ILIAS
91 if ($proxy->isActive()) {
92 $this->setOpt(CURLOPT_HTTPPROXYTUNNEL, true);
93
94 if (!empty($proxy->getHost())) {
95 $this->setOpt(CURLOPT_PROXY, $proxy->getHost());
96 } else {
97 throw new ilCurlConnectionException("Proxy host is not set on activated proxy");
98 }
99 if (!empty($proxy->getPort())) {
100 $this->setOpt(CURLOPT_PROXYPORT, $proxy->getPort());
101 }
102 }
103 }
104 return true;
105 }
106
113 final public function setOpt(int $a_option, $a_value): bool
114 {
115 if (!curl_setopt($this->ch, $a_option, $a_value)) {
116 throw new ilCurlConnectionException('Invalid option given for: ' . $a_option, curl_errno($this->ch));
117 }
118 return true;
119 }
120
125 final public function exec()
126 {
127 // Add header function
128 curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this, 'parseHeader'));
129 if (($res = curl_exec($this->ch)) === false) {
130 if (($err = curl_error($this->ch)) !== '') {
131 throw new ilCurlConnectionException($err, curl_errno($this->ch));
132 }
133
134 throw new ilCurlConnectionException('Error calling curl_exec().');
135 }
136 return $res;
137 }
138
139 public function parseResponse(string $a_response): void
140 {
141 $header_size = $this->getInfo(CURLINFO_HEADER_SIZE);
142
143 $this->header_plain = substr($a_response, 0, $header_size);
144 $this->response_body = substr($a_response, $header_size);
145 }
146
147 public function getResponseBody(): string
148 {
150 }
151
159 public function getInfo($opt = 0)
160 {
161 if ($opt) {
162 $res = curl_getinfo($this->ch, $opt);
163 } else {
164 $res = curl_getinfo($this->ch);
165 }
166 return $res;
167 }
168
175 private function parseHeader($handle, string $header): int
176 {
177 $len = strlen($header);
178 $header = explode(':', $header, 2);
179 if (count($header) === 2) { // ignore invalid headers
180 $this->header_arr[strtolower(trim($header[0]))] = trim($header[1]);
181 }
182 return $len;
183 }
184
185 final public function close(): void
186 {
187 if ($this->ch !== null) {
188 curl_close($this->ch);
189 $this->ch = null;
190 }
191 }
192
193 public function __destruct()
194 {
195 $this->close();
196 }
197}
__construct(string $a_url='')
parseHeader($handle, string $header)
Parse respone header.
getInfo($opt=0)
Get information about a specific transfer.
init(bool $set_proxy=true)
parseResponse(string $a_response)
setOpt(int $a_option, $a_value)
Wrapper for curl_setopt.
exec()
Wrapper for curl_exec.
$res
Definition: ltiservices.php:69