Documentation: Add API examples in PHP - refs BT#17801

pull/3643/head
carlos alvarado 5 years ago committed by GitHub
parent 200ce2e33b
commit 4715f3b1de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 109
      main/webservices/api/example/add_courses_session.php
  2. 110
      main/webservices/api/example/add_users_session.php
  3. 58
      main/webservices/api/example/authenticate.php
  4. 103
      main/webservices/api/example/course_agenda.php
  5. 105
      main/webservices/api/example/course_announcement.php
  6. 103
      main/webservices/api/example/course_announcements.php
  7. 103
      main/webservices/api/example/course_descriptions.php
  8. 104
      main/webservices/api/example/course_documents.php
  9. 106
      main/webservices/api/example/course_forum.php
  10. 104
      main/webservices/api/example/course_forumcategories.php
  11. 107
      main/webservices/api/example/course_forumthread.php
  12. 103
      main/webservices/api/example/course_info.php
  13. 106
      main/webservices/api/example/course_learnpath.php
  14. 113
      main/webservices/api/example/save_session.php
  15. 119
      main/webservices/api/example/save_user.php
  16. 114
      main/webservices/api/example/update_user_from_username.php
  17. 101
      main/webservices/api/example/user_courses.php
  18. 106
      main/webservices/api/example/user_message_read.php
  19. 107
      main/webservices/api/example/user_message_unread.php
  20. 102
      main/webservices/api/example/user_messages.php
  21. 102
      main/webservices/api/example/user_profile.php
  22. 102
      main/webservices/api/example/user_sessions.php
  23. 107
      main/webservices/api/example/username_exist.php

@ -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 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());
}
$jsonResponse = json_decode($response->getBody()->getContents());
if ($jsonResponse->error) {
throw new Exception('Users not assigned to session because : '.$jsonResponse->message);
}
return $jsonResponse->data[0];
}
$apiKey = authenticate();
//adding users with id 5, 6, 7 to session with id 1
if (addUsersToSession($apiKey)) {
echo 'Users successfully added';
}

@ -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';
}

@ -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;

@ -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);

@ -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);

@ -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);

@ -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);

@ -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);

@ -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);

@ -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);

@ -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);

@ -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);

@ -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);

@ -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;

@ -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;

@ -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';
}

@ -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);

@ -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);

@ -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);

@ -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);

@ -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);

@ -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);

@ -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";
}
Loading…
Cancel
Save