upgrade
This commit is contained in:
109
main/webservices/api/example/add_courses_session.php
Normal file
109
main/webservices/api/example/add_courses_session.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
require_once __DIR__.'/../../../../vendor/autoload.php';
|
||||
/**
|
||||
* Test example to user API v2.php.
|
||||
*
|
||||
* Using Guzzle' HTTP client to call the API endpoint and make requests.
|
||||
* Change URL on the first lines of addCoursesToSession() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\Client as Client;
|
||||
|
||||
// set your URL, username and password here to use it for all webservices in this test file.
|
||||
$webserviceURL = 'https://YOURCHAMILO/main/webservices/api/';
|
||||
$webserviceUsername = 'USERNAME';
|
||||
$webservicePassword = 'PASSWORD';
|
||||
|
||||
/**
|
||||
* Make a request to get the API key for admin user.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function authenticate()
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
global $webservicePassword;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
'action' => 'authenticate',
|
||||
'username' => $webserviceUsername,
|
||||
'password' => $webservicePassword,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Authentication failed because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data->apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function addCoursesToSession($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
'action' => 'add_courses_session',
|
||||
// data of the user who makes the request
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data of courses and session
|
||||
'id_session' => 1,
|
||||
'list_courses' => [
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response->getBody()->getContents());
|
||||
|
||||
if ($jsonResponse->error) {
|
||||
throw new Exception('Courses not assigned to session because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data[0];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//adding courses with id 5, 6, 7 to session with id 1
|
||||
if (addCoursesToSession($apiKey)) {
|
||||
echo 'Courses successfully added';
|
||||
}
|
||||
Reference in New Issue
Block a user