ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilCurlConnection Class Reference
+ Collaboration diagram for ilCurlConnection:

Public Member Functions

 __construct (protected string $url='', protected ?ilProxySettings $proxySettings=null,)
 
 getResponseHeader ()
 
 getResponseHeaderArray ()
 
 init (bool $set_proxy=true)
 
 setOpt (int $a_option, $a_value)
 Wrapper for curl_setopt. More...
 
 exec ()
 Wrapper for curl_exec. More...
 
 parseResponse (string $a_response)
 
 getResponseBody ()
 
 getInfo ($opt=0)
 Get information about a specific transfer. More...
 
 close ()
 
 __destruct ()
 

Static Public Member Functions

static _isCurlExtensionLoaded ()
 

Protected Attributes

 $ch = null
 

Private Member Functions

 parseHeader ($handle, string $header)
 Parse respone header. More...
 

Private Attributes

string $header_plain = ''
 
array $header_arr = array()
 
string $response_body = ''
 

Detailed Description

Definition at line 29 of file class.ilCurlConnection.php.

Constructor & Destructor Documentation

◆ __construct()

ilCurlConnection::__construct ( protected string  $url = '',
protected ?ilProxySettings  $proxySettings = null 
)

Definition at line 37 of file class.ilCurlConnection.php.

40 {
41 if (!self::_isCurlExtensionLoaded()) {
42 throw new ilCurlConnectionException('Curl extension not enabled.');
43 }
44 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...

◆ __destruct()

ilCurlConnection::__destruct ( )

Definition at line 185 of file class.ilCurlConnection.php.

186 {
187 $this->close();
188 }

References close().

+ Here is the call graph for this function:

Member Function Documentation

◆ _isCurlExtensionLoaded()

static ilCurlConnection::_isCurlExtensionLoaded ( )
staticfinal

Definition at line 46 of file class.ilCurlConnection.php.

46 : bool
47 {
48 if (!function_exists('curl_init')) {
49 return false;
50 }
51 return true;
52 }

Referenced by ilNICKeyRegisteredObjective\achieve().

+ Here is the caller graph for this function:

◆ close()

ilCurlConnection::close ( )
final

Definition at line 177 of file class.ilCurlConnection.php.

177 : void
178 {
179 if ($this->ch !== null) {
180 curl_close($this->ch);
181 $this->ch = null;
182 }
183 }

Referenced by __destruct(), and init().

+ Here is the caller graph for this function:

◆ exec()

ilCurlConnection::exec ( )
final

Wrapper for curl_exec.

Returns
bool|string

Definition at line 117 of file class.ilCurlConnection.php.

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 }
$res
Definition: ltiservices.php:69

References $res.

◆ getInfo()

ilCurlConnection::getInfo (   $opt = 0)

Get information about a specific transfer.

Parameters
intoption e.g CURLINFO_EFFECTIVE_URL
Returns
mixed

Definition at line 151 of file class.ilCurlConnection.php.

152 {
153 if ($opt) {
154 $res = curl_getinfo($this->ch, $opt);
155 } else {
156 $res = curl_getinfo($this->ch);
157 }
158 return $res;
159 }

References $res.

Referenced by parseResponse().

+ Here is the caller graph for this function:

◆ getResponseBody()

ilCurlConnection::getResponseBody ( )

Definition at line 139 of file class.ilCurlConnection.php.

139 : string
140 {
142 }

References $response_body.

◆ getResponseHeader()

ilCurlConnection::getResponseHeader ( )

Definition at line 54 of file class.ilCurlConnection.php.

54 : string
55 {
57 }

References $header_plain.

◆ getResponseHeaderArray()

ilCurlConnection::getResponseHeaderArray ( )

Definition at line 59 of file class.ilCurlConnection.php.

59 : array
60 {
61 return $this->header_arr;
62 }

References $header_arr.

◆ init()

ilCurlConnection::init ( bool  $set_proxy = true)
final

Definition at line 64 of file class.ilCurlConnection.php.

64 : 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 }
setOpt(int $a_option, $a_value)
Wrapper for curl_setopt.

References ilProxySettings\_getInstance(), close(), and setOpt().

+ Here is the call graph for this function:

◆ parseHeader()

ilCurlConnection::parseHeader (   $handle,
string  $header 
)
private

Parse respone header.

Parameters
mixed$handle
string$header
Returns
int strlen of header

Definition at line 167 of file class.ilCurlConnection.php.

167 : 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 }

◆ parseResponse()

ilCurlConnection::parseResponse ( string  $a_response)

Definition at line 131 of file class.ilCurlConnection.php.

131 : 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 }
getInfo($opt=0)
Get information about a specific transfer.

References getInfo().

+ Here is the call graph for this function:

◆ setOpt()

ilCurlConnection::setOpt ( int  $a_option,
  $a_value 
)
final

Wrapper for curl_setopt.

Parameters
int$a_option
mixed$a_value
Returns
bool

Definition at line 105 of file class.ilCurlConnection.php.

105 : 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 }

Referenced by init().

+ Here is the caller graph for this function:

Field Documentation

◆ $ch

CurlHandle resource null ilCurlConnection::$ch = null
protected

Definition at line 32 of file class.ilCurlConnection.php.

◆ $header_arr

array ilCurlConnection::$header_arr = array()
private

Definition at line 34 of file class.ilCurlConnection.php.

Referenced by getResponseHeaderArray().

◆ $header_plain

string ilCurlConnection::$header_plain = ''
private

Definition at line 33 of file class.ilCurlConnection.php.

Referenced by getResponseHeader().

◆ $response_body

string ilCurlConnection::$response_body = ''
private

Definition at line 35 of file class.ilCurlConnection.php.

Referenced by getResponseBody().


The documentation for this class was generated from the following file: