ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilCurlConnection Class Reference
+ Collaboration diagram for ilCurlConnection:

Public Member Functions

 __construct (string $a_url='')
 
 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

string $url = ''
 
 $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 35 of file class.ilCurlConnection.php.

Constructor & Destructor Documentation

◆ __construct()

ilCurlConnection::__construct ( string  $a_url = '')

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

46  {
47  $this->url = $a_url;
48 
49  if (!self::_isCurlExtensionLoaded()) {
50  throw new ilCurlConnectionException('Curl extension not enabled.');
51  }
52  }

◆ __destruct()

ilCurlConnection::__destruct ( )

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

References close().

194  {
195  $this->close();
196  }
+ Here is the call graph for this function:

Member Function Documentation

◆ _isCurlExtensionLoaded()

static ilCurlConnection::_isCurlExtensionLoaded ( )
staticfinal

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

Referenced by ilNICKeyRegisteredObjective\achieve(), and ilMediaImageUtil\getImageSize().

54  : bool
55  {
56  if (!function_exists('curl_init')) {
57  return false;
58  }
59  return true;
60  }
+ Here is the caller graph for this function:

◆ close()

ilCurlConnection::close ( )
final

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

Referenced by __destruct(), and init().

185  : void
186  {
187  if ($this->ch !== null) {
188  curl_close($this->ch);
189  $this->ch = null;
190  }
191  }
+ Here is the caller graph for this function:

◆ exec()

ilCurlConnection::exec ( )
final

Wrapper for curl_exec.

Returns
bool|string

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

References $res.

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

◆ getInfo()

ilCurlConnection::getInfo (   $opt = 0)

Get information about a specific transfer.

Parameters
intoption e.g CURLINFO_EFFECTIVE_URL
Returns
mixed

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

References $res.

Referenced by parseResponse().

160  {
161  if ($opt) {
162  $res = curl_getinfo($this->ch, $opt);
163  } else {
164  $res = curl_getinfo($this->ch);
165  }
166  return $res;
167  }
$res
Definition: ltiservices.php:69
+ Here is the caller graph for this function:

◆ getResponseBody()

ilCurlConnection::getResponseBody ( )

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

References $response_body.

147  : string
148  {
149  return $this->response_body;
150  }

◆ getResponseHeader()

ilCurlConnection::getResponseHeader ( )

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

References $header_plain.

62  : string
63  {
64  return $this->header_plain;
65  }

◆ getResponseHeaderArray()

ilCurlConnection::getResponseHeaderArray ( )

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

References $header_arr.

67  : array
68  {
69  return $this->header_arr;
70  }

◆ init()

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

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

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

Referenced by ilECSConnector\prepareConnection().

72  : 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  }
setOpt(int $a_option, $a_value)
Wrapper for curl_setopt.
+ Here is the call graph for this function:
+ Here is the caller 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 175 of file class.ilCurlConnection.php.

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

◆ parseResponse()

ilCurlConnection::parseResponse ( string  $a_response)

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

References getInfo().

139  : 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  }
getInfo($opt=0)
Get information about a specific transfer.
+ 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 113 of file class.ilCurlConnection.php.

Referenced by init().

113  : 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  }
+ Here is the caller graph for this function:

Field Documentation

◆ $ch

ilCurlConnection::$ch = null
protected

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

◆ $header_arr

array ilCurlConnection::$header_arr = array()
private

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

Referenced by getResponseHeaderArray().

◆ $header_plain

string ilCurlConnection::$header_plain = ''
private

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

Referenced by getResponseHeader().

◆ $response_body

string ilCurlConnection::$response_body = ''
private

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

Referenced by getResponseBody().

◆ $url

string ilCurlConnection::$url = ''
protected

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


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