Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation: Add API examples in PHP - refs BT#17801
- Loading branch information
1 parent
200ce2e
commit 4715f3b
Showing
23 changed files
with
2,394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
Oops, something went wrong.