PHP
PHP
bitzlato.com whoami
See more JSON Web Token (JWT) Examples
Demonstrates sending a request to the bitzlato.com whoami endpoint using an ES256 JWT token for authentication.Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\JsonObject;
use Chilkat2\Jwt;
use Chilkat2\PrivateKey;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Use the following ECC key loaded from JWK format.
$jwk = new JsonObject();
$jwk->UpdateString('kty', 'EC');
$jwk->UpdateString('crv', 'P-256');
$jwk->UpdateString('x', '...');
$jwk->UpdateString('y', '...');
$jwk->UpdateString('d', '...');
$eccKey = new PrivateKey();
try {
$eccKey->LoadJwk($jwk->Emit());
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$jwt = new Jwt();
// Build the JOSE header
$jose = new JsonObject();
$jose->AppendString('format', 'compact');
$jose->AppendString('alg', 'ES256');
// Now build the JWT claims (also known as the payload)
// Our JWT claims will contain members as shown here:
// {
// "email":"your_email@example.com",
// "aud":"usr",
// "iat":"1588286154",
// "jti":"555D9123"
// }
$claims = new JsonObject();
$claims->AppendString('jti', '555D9123');
$claims->AppendString('email', 'your_email@example.com');
// Set the timestamp of when the JWT was created to now minus 60 seconds
$curDateTime = $jwt->GenNumericDate(-60);
$claims->AddIntAt(-1, 'iat', $curDateTime);
// Set the "not process before" timestamp to now minus 60 seconds
$claims->AddIntAt(-1, 'nbf', $curDateTime);
// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 hour (3600 seconds)
$claims->AddIntAt(-1, 'exp', $curDateTime + 3600);
$claims->AppendString('aud', 'usr');
// Produce the smallest possible JWT:
$jwt->AutoCompact = true;
// Create the JWT token. This is where the RSA signature is created.
$jwt_token = $jwt->CreateJwtPk($jose->Emit(), $claims->Emit(), $eccKey);
echo $jwt_token, "\n";
// Send the HTTPS GET with the jwt_token used for Authorization.
$http = new Http();
$http->AuthToken = $jwt_token;
try {
$responseStr = $http->QuickGetStr('https://bitzlato.com/api/auth/whoami');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'status code = ', $http->LastStatus, "\n";
echo $responseStr, "\n";