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