|
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 addCoursesToSession() 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
|
|
|
/** |
|
21
|
|
|
* Make a request to get the API key for admin user. |
|
22
|
|
|
* |
|
23
|
|
|
* @throws Exception |
|
24
|
|
|
* |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
|
|
function authenticate() |
|
28
|
|
|
{ |
|
29
|
|
|
global $webserviceURL; |
|
30
|
|
|
global $webserviceUsername; |
|
31
|
|
|
global $webservicePassword; |
|
32
|
|
|
$client = new Client([ |
|
33
|
|
|
'base_uri' => $webserviceURL, |
|
34
|
|
|
]); |
|
35
|
|
|
|
|
36
|
|
|
$response = $client->post('v2.php', [ |
|
37
|
|
|
'form_params' => [ |
|
38
|
|
|
'action' => 'authenticate', |
|
39
|
|
|
'username' => $webserviceUsername, |
|
40
|
|
|
'password' => $webservicePassword, |
|
41
|
|
|
], |
|
42
|
|
|
]); |
|
43
|
|
|
|
|
44
|
|
|
if ($response->getStatusCode() !== 200) { |
|
45
|
|
|
throw new Exception('Entry denied with code : '.$response->getStatusCode()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$jsonResponse = json_decode($response->getBody()->getContents()); |
|
49
|
|
|
|
|
50
|
|
|
if ($jsonResponse->error) { |
|
51
|
|
|
throw new Exception('Authentication failed because : '.$jsonResponse->message); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $jsonResponse->data->apiKey; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $apiKey |
|
59
|
|
|
* |
|
60
|
|
|
* @throws Exception |
|
61
|
|
|
* |
|
62
|
|
|
* @return int |
|
63
|
|
|
*/ |
|
64
|
|
|
function addCoursesToSession($apiKey) |
|
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
|
|
|
'action' => 'add_courses_session', |
|
77
|
|
|
// data of the user who makes the request |
|
78
|
|
|
'username' => $webserviceUsername, |
|
79
|
|
|
'api_key' => $apiKey, |
|
80
|
|
|
// data of courses and session |
|
81
|
|
|
'id_session' => 1, |
|
82
|
|
|
'list_courses' => [ |
|
83
|
|
|
'5', |
|
84
|
|
|
'6', |
|
85
|
|
|
'7', |
|
86
|
|
|
], |
|
87
|
|
|
], |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
if ($response->getStatusCode() !== 200) { |
|
92
|
|
|
throw new Exception('Entry denied with code : '.$response->getStatusCode()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$jsonResponse = json_decode($response->getBody()->getContents()); |
|
96
|
|
|
|
|
97
|
|
|
if ($jsonResponse->error) { |
|
98
|
|
|
throw new Exception('Courses not assigned to session because : '.$jsonResponse->message); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $jsonResponse->data[0]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$apiKey = authenticate(); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
//adding courses with id 5, 6, 7 to session with id 1 |
|
107
|
|
|
if (addCoursesToSession($apiKey)) { |
|
108
|
|
|
echo 'Courses successfully added'; |
|
109
|
|
|
} |
|
110
|
|
|
|
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.