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';
|
||||
}
|
||||
110
main/webservices/api/example/add_users_session.php
Normal file
110
main/webservices/api/example/add_users_session.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?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 createUser() 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 addUsersToSession($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'add_users_session',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data for users and session
|
||||
'id_session' => 1,
|
||||
'list_users' => [
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Users not assigned to session because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data']['status'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//adding users with id 5, 6, 7 to session with id 1
|
||||
if (addUsersToSession($apiKey)) {
|
||||
echo 'Users successfully added';
|
||||
}
|
||||
58
main/webservices/api/example/authenticate.php
Normal file
58
main/webservices/api/example/authenticate.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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 createUser() 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;
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
echo 'user API Key: '.$apiKey;
|
||||
103
main/webservices/api/example/course_agenda.php
Normal file
103
main/webservices/api/example/course_agenda.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseAgenda($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_agenda',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course agenda because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of calendar events from inside the given course.
|
||||
$courseAgenda = getCourseAgenda($apiKey, 1);
|
||||
echo json_encode($courseAgenda);
|
||||
105
main/webservices/api/example/course_announcement.php
Normal file
105
main/webservices/api/example/course_announcement.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
* @param $announcementId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getCourseAnnouncement($apiKey, $courseId, $announcementId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_announcement',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
'announcement ' => $announcementId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get announcement because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the announcement published in the given course.
|
||||
$courseAnnouncement = getCourseAnnouncement($apiKey, 1, 1);
|
||||
echo json_encode($courseAnnouncement);
|
||||
103
main/webservices/api/example/course_announcements.php
Normal file
103
main/webservices/api/example/course_announcements.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseAnnouncements($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_announcements',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get announcements because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the announcements published in the given course.
|
||||
$courseAnnouncements = getCourseAnnouncements($apiKey, 1);
|
||||
echo json_encode($courseAnnouncements);
|
||||
103
main/webservices/api/example/course_descriptions.php
Normal file
103
main/webservices/api/example/course_descriptions.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseDescription($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_descriptions',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course description because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of documents in the given course.
|
||||
$courseDescription = getCourseDocuments($apiKey, 1);
|
||||
echo json_encode($courseDescription);
|
||||
104
main/webservices/api/example/course_documents.php
Normal file
104
main/webservices/api/example/course_documents.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseDocuments($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_documents',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of documents in the given course.
|
||||
$courseDescription = getCourseDocuments($apiKey, 1);
|
||||
echo json_encode($courseDescription);
|
||||
106
main/webservices/api/example/course_forum.php
Normal file
106
main/webservices/api/example/course_forum.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
* @param $forumId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseForum($apiKey, $courseId, $forumId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_forum',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
'forum' => $forumId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get details about a specific forum.
|
||||
$courseForum = getCourseForum($apiKey, 1, 1);
|
||||
echo json_encode($courseForum);
|
||||
104
main/webservices/api/example/course_forumcategories.php
Normal file
104
main/webservices/api/example/course_forumcategories.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseForumCategories($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_forumcategories',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//A list of forum categories
|
||||
$courseForumCategories = getCourseForumCategories($apiKey, 1);
|
||||
echo json_encode($courseForumCategories);
|
||||
107
main/webservices/api/example/course_forumthread.php
Normal file
107
main/webservices/api/example/course_forumthread.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
* @param $threadId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseForumThread($apiKey, $courseId, $forumId, $threadId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_forumthread',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
'forum' => $forumId,
|
||||
'thread' => $threadId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get details about a specific forum thread.
|
||||
$courseForumThread = getCourseForumThread($apiKey, 1, 1, 1);
|
||||
echo json_encode($courseForumThread);
|
||||
103
main/webservices/api/example/course_info.php
Normal file
103
main/webservices/api/example/course_info.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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 createUser() 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 getCourseInfo($apiKey, $courseId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_info',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get course info because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get information about one course in particular
|
||||
$userMessages = getCourseInfo($apiKey, 1);
|
||||
echo json_encode($userMessages);
|
||||
106
main/webservices/api/example/course_learnpath.php
Normal file
106
main/webservices/api/example/course_learnpath.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?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 createUser() 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
|
||||
* @param $courseId
|
||||
* @param $lpId
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getCourseForumThread($apiKey, $courseId, $lpId)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'course_learnpath',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'course' => $courseId,
|
||||
'lp_id' => $lpId,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get course documents because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get details about a specific forum thread.
|
||||
$courseForumThread = getCourseForumThread($apiKey, 1, 1, 1);
|
||||
echo json_encode($courseForumThread);
|
||||
113
main/webservices/api/example/save_session.php
Normal file
113
main/webservices/api/example/save_session.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?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 createUser() 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 createSession($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'save_session',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data for new session
|
||||
'name' => 'Test Session',
|
||||
'coach_username' => 1, // user_id of CoachUsername that needs to already exist in Chamilo
|
||||
'access_start_date' => '2020-01-15 15:00:00',
|
||||
'access_end_date' => '2021-01-15 15:00:00',
|
||||
'description' => 'My complete text description of the session',
|
||||
'extra' => [
|
||||
[
|
||||
'extra_Price' => '200', // the "Price" session extra field needs to be already created in Chamilo
|
||||
],
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant save session because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data']['id_session'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Creating a new session Test Session
|
||||
$sessionId = createSession($apiKey);
|
||||
echo 'ID of new session: '.$sessionId;
|
||||
119
main/webservices/api/example/save_user.php
Normal file
119
main/webservices/api/example/save_user.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?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 createUser() below to suit your needs.
|
||||
*/
|
||||
|
||||
use GuzzleHttp\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 createUser($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'save_user',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data for new user
|
||||
'firstname' => 'Test User',
|
||||
'lastname' => 'Chamilo',
|
||||
'status' => 5, // student
|
||||
'email' => 'testuser@example.com',
|
||||
'loginname' => 'restuser',
|
||||
'password' => 'restuser',
|
||||
'original_user_id_name' => 'myplatform_user_id', // field to identify the user in the external system
|
||||
'original_user_id_value' => '1234', // ID for the user in the external system
|
||||
'extra' => [
|
||||
[
|
||||
'field_name' => 'age',
|
||||
'field_value' => 29,
|
||||
],
|
||||
],
|
||||
'language' => 'english',
|
||||
//'phone' => '',
|
||||
//'expiration_date' => '',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
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('User not created because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data[0];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Creating a new user restuser
|
||||
$userId = createUser($apiKey);
|
||||
echo 'ID of new user: '.$userId;
|
||||
114
main/webservices/api/example/update_user_from_username.php
Normal file
114
main/webservices/api/example/update_user_from_username.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?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 createUser() 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 updateUserFromUsername($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post('v2.php', [
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'update_user_from_username',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
// data for the user to be updated
|
||||
'loginName' => 'TestUser',
|
||||
'firstname' => 'Test User',
|
||||
'lastname' => 'Chamilo',
|
||||
'status' => 5, // student
|
||||
'email' => 'testuser@example.com',
|
||||
'enabled' => 1,
|
||||
'extra' => [
|
||||
[
|
||||
'field_name' => 'age', // The "age" user extra field needs to already be created on Chamilo
|
||||
'field_value' => 35,
|
||||
],
|
||||
],
|
||||
'language' => 'english',
|
||||
'expiration_date' => '2025-12-31 23:59:59',
|
||||
],
|
||||
]);
|
||||
|
||||
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('User not updated because : '.$jsonResponse->message);
|
||||
}
|
||||
|
||||
return $jsonResponse->data[0];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//update user TestUser
|
||||
if (updateUserFromUsername($apiKey)) {
|
||||
echo 'User updated successfully';
|
||||
}
|
||||
101
main/webservices/api/example/user_courses.php
Normal file
101
main/webservices/api/example/user_courses.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?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 createUser() 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 getUserCourses($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_courses',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get user courses because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get a list of courses of the user calling this service.
|
||||
$userMessages = getUserCourses($apiKey);
|
||||
echo json_encode($userMessages);
|
||||
106
main/webservices/api/example/user_message_read.php
Normal file
106
main/webservices/api/example/user_message_read.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?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 createUser() 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 getUserMessageRead($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_message_read',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'messages' => [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get read messages because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Mark a specific message (in the social network interface) as read.
|
||||
$userMessages = getUserMessageRead($apiKey);
|
||||
echo json_encode($userMessages);
|
||||
107
main/webservices/api/example/user_message_unread.php
Normal file
107
main/webservices/api/example/user_message_unread.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?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 createUser() 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 getUserMessageUnread($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_message_unread',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'messages' => [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get unread messages because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Mark a specific message (in the social network interface) as "unread".
|
||||
$userMessages = getUserMessageUnread($apiKey);
|
||||
echo json_encode($userMessages);
|
||||
102
main/webservices/api/example/user_messages.php
Normal file
102
main/webservices/api/example/user_messages.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?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 createUser() 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 getUserMessages($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_messages',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('Cant get user messages because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Read user mesages
|
||||
$userMessages = getUserMessages($apiKey);
|
||||
echo json_encode($userMessages);
|
||||
102
main/webservices/api/example/user_profile.php
Normal file
102
main/webservices/api/example/user_profile.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?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 createUser() 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 getUserProfile($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_profile',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get user profile because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of calendar events from inside the given course.
|
||||
$userProfile = getUserProfile($apiKey);
|
||||
echo json_encode($userProfile);
|
||||
102
main/webservices/api/example/user_sessions.php
Normal file
102
main/webservices/api/example/user_sessions.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?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 createUser() 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 getUserSessions($apiKey)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'user_sessions',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get user profile because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Get the list of sessions of the current user
|
||||
$userSessions = getUserSessions($apiKey);
|
||||
echo json_encode($userSessions);
|
||||
107
main/webservices/api/example/username_exist.php
Normal file
107
main/webservices/api/example/username_exist.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?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 createUser() 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 getUserNameExist($apiKey, $loginname)
|
||||
{
|
||||
global $webserviceURL;
|
||||
global $webserviceUsername;
|
||||
$client = new Client([
|
||||
'base_uri' => $webserviceURL,
|
||||
]);
|
||||
|
||||
$response = $client->post(
|
||||
'v2.php',
|
||||
[
|
||||
'form_params' => [
|
||||
// data for the user who makes the request
|
||||
'action' => 'username_exist',
|
||||
'username' => $webserviceUsername,
|
||||
'api_key' => $apiKey,
|
||||
'loginname' => $loginname,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Entry denied with code : '.$response->getStatusCode());
|
||||
}
|
||||
|
||||
$content = $response->getBody()->getContents();
|
||||
$jsonResponse = json_decode($content, true);
|
||||
|
||||
if ($jsonResponse['error']) {
|
||||
throw new Exception('cant get user profile because : '.$jsonResponse['message']);
|
||||
}
|
||||
|
||||
return $jsonResponse['data'][0];
|
||||
}
|
||||
|
||||
$apiKey = authenticate();
|
||||
|
||||
//Return if a username already exist
|
||||
$userNameExist = getUserNameExist($apiKey, 'admin');
|
||||
if ($userNameExist == true) {
|
||||
echo "User name exist";
|
||||
} else {
|
||||
echo "User doesnt name exist";
|
||||
}
|
||||
Reference in New Issue
Block a user