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