ILIAS  release_7 Revision v7.30-3-g800a261c036
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 } catch (Exception $e) {
61 ilObjCmiXapi::log()->error($e->getMessage());
62 throw new Exception("LRS Connection Problems");
63 }
64 }
65
66 public static function checkResponse($response, &$body, $allowedStatus = [200, 204])
67 {
68 if ($response['state'] == 'fulfilled') {
69 $status = $response['value']->getStatusCode();
70 if (in_array($status, $allowedStatus)) {
71 $body = $response['value']->getBody();
72 return true;
73 } else {
74 ilObjCmiXapi::log()->error("LRS error: " . $response['value']->getBody());
75 return false;
76 }
77 } else {
78 try {
79 ilObjCmiXapi::log()->error("Connection error: " . $response['reason']->getMessage());
80 } catch (Exception $e) {
81 ilObjCmiXapi::log()->error('error:' . $e->getMessage());
82 }
83 return false;
84 }
85 }
86
87 public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986)
88 {
89 if (!$params) {
90 return '';
91 }
92
93 if ($encoding === false) {
94 $encoder = function ($str) {
95 return $str;
96 };
97 } elseif ($encoding === PHP_QUERY_RFC3986) {
98 $encoder = 'rawurlencode';
99 } elseif ($encoding === PHP_QUERY_RFC1738) {
100 $encoder = 'urlencode';
101 } else {
102 throw new \InvalidArgumentException('Invalid type');
103 }
104
105 $qs = '';
106 foreach ($params as $k => $v) {
107 $k = $encoder($k);
108 if (!is_array($v)) {
109 $qs .= $k;
110 if ($v !== null) {
111 $qs .= '=' . $encoder($v);
112 }
113 $qs .= '&';
114 } else {
115 foreach ($v as $vv) {
116 $qs .= $k;
117 if ($vv !== null) {
118 $qs .= '=' . $encoder($vv);
119 }
120 $qs .= '&';
121 }
122 }
123 }
124 return $qs ? (string) substr($qs, 0, -1) : '';
125 }
126}
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