Issues (2029)

main/webservices/api/example/course_agenda.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
require_once __DIR__.'/../../../../vendor/autoload.php';
6
/**
7
 * Test example to user API v2.php.
8
 *
9
 * Using Guzzle' HTTP client to call the API endpoint and make requests.
10
 * Change URL on the first lines of createUser() below to suit your needs.
11
 */
12
13
use GuzzleHttp\Client as Client;
14
15
// set your URL, username and password here to use it for all webservices in this test file.
16
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
17
$webserviceUsername = 'USERNAME';
18
$webservicePassword = 'PASSWORD';
19
/**
20
 * Make a request to get the API key for admin user.
21
 *
22
 * @throws Exception
23
 *
24
 * @return string
25
 */
26
function authenticate()
27
{
28
    global $webserviceURL;
29
    global $webserviceUsername;
30
    global $webservicePassword;
31
    $client = new Client([
32
        'base_uri' => $webserviceURL,
33
    ]);
34
35
    $response = $client->post('v2.php', [
36
        'form_params' => [
37
            'action' => 'authenticate',
38
            'username' => $webserviceUsername,
39
            'password' => $webservicePassword,
40
        ],
41
    ]);
42
43
    if ($response->getStatusCode() !== 200) {
44
        throw new Exception('Entry denied with code : '.$response->getStatusCode());
45
    }
46
47
    $jsonResponse = json_decode($response->getBody()->getContents());
48
49
    if ($jsonResponse->error) {
50
        throw new Exception('Authentication failed because : '.$jsonResponse->message);
51
    }
52
53
    return $jsonResponse->data->apiKey;
54
}
55
56
/**
57
 * @param $apiKey
58
 * @param $courseId
59
 *
60
 * @throws Exception
61
 *
62
 * @return array
63
 */
64
function getCourseAgenda($apiKey, $courseId)
65
{
66
    global $webserviceURL;
67
    global $webserviceUsername;
68
    $client = new Client([
69
        'base_uri' => $webserviceURL,
70
    ]);
71
72
    $response = $client->post(
73
        'v2.php',
74
        [
75
            'form_params' => [
76
                // data for the user who makes the request
77
                'action' => 'course_agenda',
78
                'username' => $webserviceUsername,
79
                'api_key' => $apiKey,
80
                'course' => $courseId,
81
            ],
82
        ]
83
    );
84
85
    if ($response->getStatusCode() !== 200) {
86
        throw new Exception('Entry denied with code : '.$response->getStatusCode());
87
    }
88
89
    $content = $response->getBody()->getContents();
90
    $jsonResponse = json_decode($content, true);
91
92
    if ($jsonResponse['error']) {
93
        throw new Exception('cant get course agenda because : '.$jsonResponse['message']);
94
    }
95
96
    return $jsonResponse['data'];
97
}
98
99
$apiKey = authenticate();
0 ignored issues
show
The call to authenticate() has too few arguments starting with WSUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
$apiKey = /** @scrutinizer ignore-call */ authenticate();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
100
101
//Get the list of calendar events from inside the given course.
102
$courseAgenda = getCourseAgenda($apiKey, 1);
103
echo json_encode($courseAgenda);
104