ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCurlConnection.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
32 protected $ch = null;
33 private string $header_plain = '';
34 private array $header_arr = array();
35 private string $response_body = '';
36
37 public function __construct(
38 protected string $url = '',
39 protected ?ilProxySettings $proxySettings = null,
40 ) {
41 if (!self::_isCurlExtensionLoaded()) {
42 throw new ilCurlConnectionException('Curl extension not enabled.');
43 }
44 }
45
46 final public static function _isCurlExtensionLoaded(): bool
47 {
48 if (!function_exists('curl_init')) {
49 return false;
50 }
51 return true;
52 }
53
54 public function getResponseHeader(): string
55 {
57 }
58
59 public function getResponseHeaderArray(): array
60 {
61 return $this->header_arr;
62 }
63
64 final public function init(bool $set_proxy = true): bool
65 {
66 // teminate existing handles
67 $this->close();
68 if ($this->url !== '') {
69 $this->ch = curl_init($this->url);
70 } else {
71 $this->ch = curl_init();
72 }
73 if (!$this->ch) {
74 throw new ilCurlConnectionException('Cannot init curl connection.');
75 }
76 if (curl_errno($this->ch)) {
77 throw new ilCurlConnectionException(curl_error($this->ch), curl_errno($this->ch));
78 }
79
80 if ($set_proxy) {
81 // use a proxy, if configured by ILIAS
82 $proxy = $this->proxySettings ?? ilProxySettings::_getInstance();
83 if ($proxy->isActive()) {
84 $this->setOpt(CURLOPT_HTTPPROXYTUNNEL, true);
85
86 if (!empty($proxy->getHost())) {
87 $this->setOpt(CURLOPT_PROXY, $proxy->getHost());
88 } else {
89 throw new ilCurlConnectionException("Proxy host is not set on activated proxy");
90 }
91 if (!empty($proxy->getPort())) {
92 $this->setOpt(CURLOPT_PROXYPORT, $proxy->getPort());
93 }
94 }
95 }
96 return true;
97 }
98
105 final public function setOpt(int $a_option, $a_value): bool
106 {
107 if (!curl_setopt($this->ch, $a_option, $a_value)) {
108 throw new ilCurlConnectionException('Invalid option given for: ' . $a_option, curl_errno($this->ch));
109 }
110 return true;
111 }
112
117 final public function exec()
118 {
119 // Add header function
120 curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this, 'parseHeader'));
121 if (($res = curl_exec($this->ch)) === false) {
122 if (($err = curl_error($this->ch)) !== '') {
123 throw new ilCurlConnectionException($err, curl_errno($this->ch));
124 }
125
126 throw new ilCurlConnectionException('Error calling curl_exec().');
127 }
128 return $res;
129 }
130
131 public function parseResponse(string $a_response): void
132 {
133 $header_size = $this->getInfo(CURLINFO_HEADER_SIZE);
134
135 $this->header_plain = substr($a_response, 0, $header_size);
136 $this->response_body = substr($a_response, $header_size);
137 }
138
139 public function getResponseBody(): string
140 {
142 }
143
151 public function getInfo($opt = 0)
152 {
153 if ($opt) {
154 $res = curl_getinfo($this->ch, $opt);
155 } else {
156 $res = curl_getinfo($this->ch);
157 }
158 return $res;
159 }
160
167 private function parseHeader($handle, string $header): int
168 {
169 $len = strlen($header);
170 $header = explode(':', $header, 2);
171 if (count($header) === 2) { // ignore invalid headers
172 $this->header_arr[strtolower(trim($header[0]))] = trim($header[1]);
173 }
174 return $len;
175 }
176
177 final public function close(): void
178 {
179 if ($this->ch !== null) {
180 curl_close($this->ch);
181 $this->ch = null;
182 }
183 }
184
185 public function __destruct()
186 {
187 $this->close();
188 }
189}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
parseHeader($handle, string $header)
Parse respone header.
getInfo($opt=0)
Get information about a specific transfer.
init(bool $set_proxy=true)
__construct(protected string $url='', protected ?ilProxySettings $proxySettings=null,)
parseResponse(string $a_response)
setOpt(int $a_option, $a_value)
Wrapper for curl_setopt.
exec()
Wrapper for curl_exec.
class ilProxySettings
$res
Definition: ltiservices.php:69
$url
Definition: shib_logout.php:68