1. Go to this page and download the library: Download xzawed/keycloak-sdk 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/ */
xzawed / keycloak-sdk example snippets
use Xzawed\Keycloak\{KeycloakClient, KeycloakConfig}; // admin lives under Xzawed\Keycloak\Admin
declare(strict_types=1);
tion\User;
use Xzawed\Keycloak\KeycloakClient;
use Xzawed\Keycloak\KeycloakConfig;
$client = KeycloakClient::create(new KeycloakConfig(
serverUrl: 'https://kc.example.com',
realm: 'myrealm',
clientId: 'my-app',
clientSecret: '…', // load from an env var / secret manager; __toString is auto-masked
));
// 1) client-credentials grant. TokenSet::__toString() masks the tokens (accessToken=***).
$token = $client->auth()->clientCredentialsToken();
echo "token type: {$token->tokenType}, expires in: {$token->expiresIn}s\n";
// 2) hardened verification (alg pinning · exact iss · aud containment · mandatory exp · clock skew).
$validated = $client->auth()->validate($token->accessToken);
echo "subject: {$validated->subject}, issuer: {$validated->issuer}\n";
// 3) admin API — create/update return void (sister-language isomorphism). Look the id up with findIdByUsername().
$users = $client->admin()->users();
$users->create(new User(username: 'alice', enabled: true));
$userId = $users->findIdByUsername('alice');
$users->update($userId, $users->get($userId)->withEmail('[email protected]'));
echo "created userId={$userId}\n";
// before — the one-argument form could only update a role in place
$admin->roles()->update(new Role(name: 'reporting'));
// now — address by the current name, put the new one in the body
$admin->roles()->update('reporting', new Role(name: 'analytics'));
// updating without renaming: repeat the name
$admin->roles()->update('reporting', new Role(name: 'reporting', description: 'Read-only'));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.