PHP code example of klsoft / php-keycloak-client

1. Go to this page and download the library: Download klsoft/php-keycloak-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

klsoft / php-keycloak-client example snippets


use Klsoft\KeycloakClient\KeycloakClient;

$keycloakClient = new KeycloakClient(
    "http://localhost:8080/realms/myrealm", //Keycloak realm URL
    "Keycloak client ID",
    "http://localhost/login", //Keycloak client redirect URI
    "Keycloak client secret"); //This is optional, but it is 

<a  href="<?=  $keycloakClient->createAuthorizationCodeLoginUrl()  

<a  href="<?=  $keycloakClient->createImplicitLoginUrl()  

$queryParams = $request->getQueryParams();
if (isset($queryParams['code'])) {
    $responseResult = $keycloakClient->fetchTokenByAuthorizationCode($queryParams['code"']);
    if ($responseResult->responseStatusCode == 200) {
        $data = $responseResult->data;
        $identityData = $this->extractIndentityData($data->id_token);
        $identityRepository->save(new User(
            $identityData->sub, 
            $identityData->preferred_username, 
            $identityData->email, 
            $data->access_token, 
            $data->refresh_token));
        $identity = $identityRepository->findIdentity($identityData->sub)    
        $authManager->login($identity);
    } 
    elseif ($responseResult->responseStatusCode == 401) {
        //Unauthorized
    }
    else {
        //Something got wrong
    }
}

$responseResult = $keycloakClient->fetchTokenByClientCredentials();
if ($responseResult->responseStatusCode == 200) {
    $data = $responseResult->data;
} 
elseif ($responseResult->responseStatusCode == 401) {
    //Unauthorized
}
else {
    //Something got wrong
}

$responseResult = $keycloakClient->refreshToken($authManager->identity->refresh_token);
if ($responseResult->responseStatusCode == 200) {
    $data = $responseResult->data;
    $identityRepository->findIdentity($identityData->sub);
    $identityData = $this->extractIndentityData($data->id_token);
    {
        $data = $responseResult->data;
        $identityData = $this->extractIndentityData(data->id_token);
        $user = $identityRepository->findIdentity($identityData->sub);
        $user->access_token = $data->access_token;
        $user->refresh_token = $data->refresh_token;
        $identityRepository->save($user);
    }
} 
elseif ($responseResult->responseStatusCode == 401) {
    //Unauthorized
}
else {
    //Something got wrong
}

$responseResult = $keycloakClient->fetchRequestingPartyTokenByPermissionTicket(
    $authManager->identity->access_token,
    "permission ticket");
if ($responseResult->responseStatusCode == 200) {
    $rpt = $responseResult->data->access_token;
} 
elseif ($responseResult->responseStatusCode == 401) {
    //Unauthorized
}
else {
    //Something got wrong
}

$responseResult = $keycloakClient->fetchRequestingPartyTokenByClientId($authManager->identity->access_token);
if ($responseResult->responseStatusCode == 200) {
    $rpt = $responseResult->data->access_token;
} 
elseif ($responseResult->responseStatusCode == 401) {
    //Unauthorized
}
else {
    //Something got wrong
}

$responseResult = $keycloakClient->fetchUserInfo($authManager->identity->access_token);
if ($responseResult->responseStatusCode == 200) {
    $data = $responseResult->data;
}
elseif ($responseResult->responseStatusCode == 401) {
    //Unauthorized
}
else {
    //Something got wrong
}

$responseResult = $keycloakClient->logout($authManager->identity->refresh_token);
if ($responseResult->responseStatusCode == 204) {
    $authManager->logout();
} 
elseif ($responseResult->responseStatusCode == 401) {
    //Unauthorized
}
else {
    //Something got wrong
}
bash
composer