ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilCmiXapiAbstractRequest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31{
32 private string $basicAuth;
33 public static bool $plugin = false;
34
38 public function __construct(string $basicAuth)
39 {
40 $this->basicAuth = $basicAuth;
41 }
42
43 protected function sendRequest(string $url): string
44 {
45 ilObjCmiXapi::log()->debug($url);
46
47 // Header wie bei Guzzle
48 $headers = [
49 'Authorization: ' . $this->basicAuth,
50 'X-Experience-API-Version: 1.0.3'
51 ];
52
53 // cURL initialisieren
54 $ch = curl_init($url);
55 curl_setopt_array($ch, [
56 CURLOPT_RETURNTRANSFER => true,
57 CURLOPT_HTTPHEADER => $headers,
58 CURLOPT_CONNECTTIMEOUT => 10,
59 CURLOPT_TIMEOUT => 30,
60 CURLOPT_SSL_VERIFYPEER => true,
61 CURLOPT_FOLLOWLOCATION => true
62 ]);
63
64 // Anfrage ausführen
65 $body = curl_exec($ch);
66 $error = curl_error($ch);
67 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
68 curl_close($ch);
69
70 // Fehlerbehandlung
71 if ($error) {
72 ilObjCmiXapi::log()->error("cURL error: " . $error);
73 throw new Exception("LRS Connection Problems: " . $error);
74 }
75
76 if ($httpCode < 200 || $httpCode >= 300) {
77 ilObjCmiXapi::log()->error("Unexpected HTTP status: {$httpCode}");
78 throw new Exception("LRS Connection Problems (HTTP {$httpCode})");
79 }
80
81 if ($body === false || $body === '') {
82 ilObjCmiXapi::log()->error("Empty response body from LRS");
83 throw new Exception("Empty response from LRS");
84 }
85
86 return $body;
87 }
88
89 //todo body?
90 public static function checkResponse(array $response, &$body, array $allowedStatus = [200, 204]): bool
91 {
92 if ($response['state'] == 'fulfilled') {
93 $status = $response['value']->getStatusCode();
94 if (in_array($status, $allowedStatus)) {
95 $body = $response['value']->getBody()->getContents();
96 return true;
97 } else {
98 ilObjCmiXapi::log()->error("LRS error: " . $response['value']->getBody()->getContents());
99 return false;
100 }
101 } else {
102 try {
103 ilObjCmiXapi::log()->error("Connection error: " . $response['reason']->getMessage());
104 } catch (Exception $e) {
105 ilObjCmiXapi::log()->error('error:' . $e->getMessage());
106 }
107 return false;
108 }
109 }
110
111 //todo
112 public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
113 {
114 if ($params === []) {
115 return '';
116 }
117
118 if ($encoding === false) {
119 $encoder = fn($str) => $str;
120 } elseif ($encoding === PHP_QUERY_RFC3986) {
121 $encoder = 'rawurlencode';
122 } elseif ($encoding === PHP_QUERY_RFC1738) {
123 $encoder = 'urlencode';
124 } else {
125 throw new \InvalidArgumentException('Invalid type');
126 }
127
128 $qs = '';
129 foreach ($params as $k => $v) {
130 $k = $encoder($k);
131 if (!is_array($v)) {
132 $qs .= $k;
133 if ($v !== null) {
134 $qs .= '=' . $encoder($v);
135 }
136 $qs .= '&';
137 } else {
138 foreach ($v as $vv) {
139 $qs .= $k;
140 if ($vv !== null) {
141 $qs .= '=' . $encoder($vv);
142 }
143 $qs .= '&';
144 }
145 }
146 }
147 return $qs ? (string) substr($qs, 0, -1) : '';
148 }
149}
__construct(string $basicAuth)
ilCmiXapiAbstractRequest constructor.
static buildQuery(array $params, $encoding=PHP_QUERY_RFC3986)
static checkResponse(array $response, &$body, array $allowedStatus=[200, 204])
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
$url
Definition: shib_logout.php:68
$response
Definition: xapitoken.php:90