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