ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCmiXapiAbstractRequest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
16{
20 private $basicAuth;
21
25 public static $plugin = false;
26
31 public function __construct(string $basicAuth)
32 {
33 $this->basicAuth = $basicAuth;
34 }
35
40 protected function sendRequest($url)
41 {
42 $client = new GuzzleHttp\Client();
43 $req_opts = array(
44 GuzzleHttp\RequestOptions::VERIFY => true,
45 GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 10,
46 GuzzleHttp\RequestOptions::HTTP_ERRORS => false
47 );
48 ilObjCmiXapi::log()->debug($url);
49 $request = new GuzzleHttp\Psr7\Request('GET', $url, [
50 'Authorization' => $this->basicAuth,
51 'X-Experience-API-Version' => '1.0.3'
52 ]);
53 try {
54 $body = '';
55 $promises = array();
56 $promises['default'] = $client->sendAsync($request, $req_opts);
57 $responses = GuzzleHttp\Promise\Utils::settle($promises)->wait();
58 self::checkResponse($responses['default'],$body);
59 return $body;
60 }
61 catch(Exception $e) {
62 ilObjCmiXapi::log()->error($e->getMessage());
63 throw new Exception("LRS Connection Problems");
64 }
65 }
66
67 public static function checkResponse($response, &$body, $allowedStatus = [200, 204])
68 {
69 if ($response['state'] == 'fulfilled')
70 {
71 $status = $response['value']->getStatusCode();
72 if (in_array($status,$allowedStatus))
73 {
74 $body = $response['value']->getBody();
75 return true;
76 }
77 else
78 {
79 ilObjCmiXapi::log()->error("LRS error: " . $response['value']->getBody());
80 return false;
81 }
82 }
83 else {
84 try
85 {
86 ilObjCmiXapi::log()->error("Connection error: " . $response['reason']->getMessage());
87 }
88 catch(Exception $e)
89 {
90 ilObjCmiXapi::log()->error('error:' . $e->getMessage());
91 }
92 return false;
93 }
94 }
95
96 public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986)
97 {
98 if (!$params) {
99 return '';
100 }
101
102 if ($encoding === false) {
103 $encoder = function ($str) {
104 return $str;
105 };
106 } elseif ($encoding === PHP_QUERY_RFC3986) {
107 $encoder = 'rawurlencode';
108 } elseif ($encoding === PHP_QUERY_RFC1738) {
109 $encoder = 'urlencode';
110 } else {
111 throw new \InvalidArgumentException('Invalid type');
112 }
113
114 $qs = '';
115 foreach ($params as $k => $v) {
116 $k = $encoder($k);
117 if (!is_array($v)) {
118 $qs .= $k;
119 if ($v !== null) {
120 $qs .= '=' . $encoder($v);
121 }
122 $qs .= '&';
123 } else {
124 foreach ($v as $vv) {
125 $qs .= $k;
126 if ($vv !== null) {
127 $qs .= '=' . $encoder($vv);
128 }
129 $qs .= '&';
130 }
131 }
132 }
133 return $qs ? (string) substr($qs, 0, -1) : '';
134 }
135}
An exception for terminatinating execution or to throw for unit testing.
__construct(string $basicAuth)
ilCmiXapiAbstractRequest constructor.
static buildQuery(array $params, $encoding=PHP_QUERY_RFC3986)
static checkResponse($response, &$body, $allowedStatus=[200, 204])
if($_SERVER['argc']< 4) $client
Definition: cron.php:12
$url
$response