Sample code for 30+ languages & platforms
PHP ActiveX Requires Chilkat v11.6.0+

Azure Table: Read an Entity with Entra Client Credentials

See more Azure Table Service Examples

Supply tenant_id (directory GUID), client_id and client_secret in private storage.json. This confidential-client flow requests the Storage .default scope; it has no refresh token. Assign Storage Table Data Reader or an appropriate Table data role. A management-plane Contributor role alone is not sufficient. Acquires a Storage-audience token and actually uses it to retrieve an entity, without an account key.

Saves only access_token, expires_at and resource to private table-token.json, replacing the prior cache. Uses request-start time for expiry. Reacquire before expiry; do not print the token or embed a client secret in a public client. This sample stops on identity errors rather than applying a generic retry policy.

Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.

HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.

Microsoft reference.

Chilkat PHP ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

//  StringBuilder compares text by value in every generated language.
$textCompare = new COM("Chilkat.StringBuilder");
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$config = new COM("Chilkat.JsonObject");
$config->Strict = 1;
$success = $config->LoadFile('qa_data/azure/storage.json');
if ($success != 1) {
    print 'Could not load private Azure configuration.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($config->stringOf('account'));
$sameText1 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('account') != 1) or ($sameText1 == 1)) {
    print 'Supply the storage account name.' . "\n";
    exit;
}

$identity = new COM("Chilkat.Rest");
$identity->ConnectTimeoutMs = 5000;
$identity->IdleTimeoutMs = 30000;
$expires = new COM("Chilkat.CkDateTime");
$success = $expires->SetFromCurrentSystemTime();
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($config->stringOf('tenant_id'));
$sameText2 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('tenant_id') != 1) or ($sameText2 == 1)) {
    print 'Missing string tenant_id.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($config->stringOf('client_id'));
$sameText3 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('client_id') != 1) or ($sameText3 == 1)) {
    print 'Missing string client_id.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($config->stringOf('client_secret'));
$sameText4 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('client_secret') != 1) or ($sameText4 == 1)) {
    print 'Missing string client_secret.' . "\n";
    exit;
}

$validate1 = new COM("Chilkat.StringBuilder");
$alphabet1 = new COM("Chilkat.StringBuilder");
$success = $validate1->Append($config->stringOf('tenant_id'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $alphabet1->Append('0123456789abcdefABCDEF-');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

if (($validate1->Length < 36) or ($validate1->Length > 36)) {
    print 'Invalid tenant GUID length.' . "\n";
    exit;
}

$index1 = 0;
while (($index1 < $validate1->Length)) {
    if ($alphabet1->Contains($validate1->getRange($index1,1,0),1) != 1) {
        print 'Invalid tenant GUID.' . "\n";
        exit;
    }

    $index1 = $index1 + 1;
}

$identityTls = new COM("Chilkat.Socket");
$identityTls->RequireSslCertVerify = 1;
$identityTls->MaxReadIdleMs = 30000;
$identityTls->MaxSendIdleMs = 30000;
$success = $identityTls->Connect('login.microsoftonline.com',443,1,20000);
if ($success != 1) {
    print 'Identity TLS connection failed.' . "\n";
    exit;
}

$success = $identity->UseConnection($identityTls,0);
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$tokenPath = new COM("Chilkat.StringBuilder");
$success = $tokenPath->Append('/');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $tokenPath->Append($config->stringOf('tenant_id'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $tokenPath->Append('/oauth2/v2.0/token');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $identity->AddQueryParam('grant_type','client_credentials');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $identity->AddQueryParam('scope','https://storage.azure.com/.default');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $identity->AddQueryParam('client_id',$config->stringOf('client_id'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $identity->AddQueryParam('client_secret',$config->stringOf('client_secret'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

//  FullRequestFormUrlEncoded places the parameters in the POST body, not the URL.
$tokenText = $identity->fullRequestFormUrlEncoded('POST',$tokenPath->getAsString());
if ($identity->LastMethodSuccess != 1) {
    print 'Token request did not complete.' . "\n";
    exit;
}

if ($identity->ResponseStatusCode != 200) {
    print 'Token request failed; keep its body and diagnostics private.' . "\n";
    exit;
}

$token = new COM("Chilkat.JsonObject");
$token->Strict = 1;
$success = $token->Load($tokenText);
if ($success != 1) {
    print 'Expected token JSON.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($token->stringOf('access_token'));
$sameText5 = $textCompare->ContentsEqual('',1);
if (($token->JsonTypeOf('access_token') != 1) or ($sameText5 == 1)) {
    print 'Missing string access_token.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($token->stringOf('token_type'));
$sameText6 = $textCompare->ContentsEqual('Bearer',1);
if ($sameText6 != 1) {
    print 'Expected a bearer token.' . "\n";
    exit;
}

if (($token->JsonTypeOf('expires_in') != 2)) {
    print 'Invalid token lifetime type.' . "\n";
    exit;
}

$validate2 = new COM("Chilkat.StringBuilder");
$alphabet2 = new COM("Chilkat.StringBuilder");
$success = $validate2->Append($token->stringOf('expires_in'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $alphabet2->Append('0123456789');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

if (($validate2->Length < 1) or ($validate2->Length > 6)) {
    print 'Invalid token lifetime length.' . "\n";
    exit;
}

$index2 = 0;
while (($index2 < $validate2->Length)) {
    if ($alphabet2->Contains($validate2->getRange($index2,1,0),1) != 1) {
        print 'Invalid token lifetime.' . "\n";
        exit;
    }

    $index2 = $index2 + 1;
}

$seconds = $token->IntOf('expires_in');
if (($seconds < 120) or ($seconds > 86400)) {
    print 'Unexpected token lifetime.' . "\n";
    exit;
}

$success = $expires->AddSeconds($seconds);
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$now = new COM("Chilkat.CkDateTime");
$success = $now->SetFromCurrentSystemTime();
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

if ($expires->DiffSeconds($now) <= 60) {
    print 'Token expired while the request was in progress.' . "\n";
    exit;
}

$saved = new COM("Chilkat.JsonObject");
$success = $saved->UpdateString('access_token',$token->stringOf('access_token'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $saved->UpdateString('expires_at',$expires->getAsTimestamp(0));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $saved->UpdateString('resource','https://storage.azure.com/');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $saved->WriteFile('qa_data/azure/table-token.json');
if ($success != 1) {
    print 'Could not save the private token file.' . "\n";
    exit;
}

$accountText = new COM("Chilkat.StringBuilder");
$success = $accountText->Append($config->stringOf('account'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

if (($accountText->Length < 3) or ($accountText->Length > 24)) {
    print 'Storage account names need 3-24 lowercase letters or digits.' . "\n";
    exit;
}

$allowedAccountChars = new COM("Chilkat.StringBuilder");
$success = $allowedAccountChars->Append('abcdefghijklmnopqrstuvwxyz0123456789');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$charIndex = 0;
while (($charIndex < $accountText->Length)) {
    if ($allowedAccountChars->Contains($accountText->getRange($charIndex,1,0),1) != 1) {
        print 'Storage account names need lowercase letters or digits.' . "\n";
        exit;
    }

    $charIndex = $charIndex + 1;
}

$endpoint = new COM("Chilkat.StringBuilder");
$success = $endpoint->Append($config->stringOf('account'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $endpoint->Append('.table.core.windows.net');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

//  Table names contain only letters and digits and are case-insensitive.
//  Entity PartitionKey and RowKey values are case-sensitive.
$resourcePath = '/ExampleCustomers()';
$rest = new COM("Chilkat.Rest");
$rest->ConnectTimeoutMs = 20000;
$rest->IdleTimeoutMs = 60000;
$connection = new COM("Chilkat.Socket");
$connection->RequireSslCertVerify = 1;
$connection->MaxReadIdleMs = 60000;
$connection->MaxSendIdleMs = 60000;
$success = $connection->Connect($endpoint->getAsString(),443,1,20000);
if ($success != 1) {
    print 'TLS connection failed; inspect diagnostics privately.' . "\n";
    exit;
}

//  Do not replay a write automatically after losing its response.
$success = $rest->UseConnection($connection,0);
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$result = new COM("Chilkat.JsonObject");
$result->Strict = 1;
$success = $rest->AddHeader('DataServiceVersion','3.0;NetFx');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $rest->AddHeader('MaxDataServiceVersion','3.0;NetFx');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $rest->AddHeader('Content-Type','application/json');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $rest->AddHeader('Accept','application/json;odata=minimalmetadata');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $rest->AddHeader('x-ms-version','2026-04-06');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$authorization = new COM("Chilkat.StringBuilder");
$success = $authorization->Append('Bearer ');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $authorization->Append($token->stringOf('access_token'));
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $rest->AddHeader('Authorization',$authorization->getAsString());
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$partitionKey = 'customers-west';
$rowKey = 'customer-001';
$encodedPk = new COM("Chilkat.StringBuilder");
$success = $encodedPk->Append($partitionKey);
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$escapedencodedPk = $encodedPk->Replace(''','''');
$success = $encodedPk->Encode('url_rfc3986','utf-8');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$encodedRk = new COM("Chilkat.StringBuilder");
$success = $encodedRk->Append($rowKey);
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$escapedencodedRk = $encodedRk->Replace(''','''');
$success = $encodedRk->Encode('url_rfc3986','utf-8');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

//  Escape apostrophes for OData first, then percent-encode each key once.
$entityPath = new COM("Chilkat.StringBuilder");
$success = $entityPath->Append('/ExampleCustomers(PartitionKey='');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $entityPath->Append($encodedPk->getAsString());
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $entityPath->Append('',RowKey='');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $entityPath->Append($encodedRk->getAsString());
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$success = $entityPath->Append('')');
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

$resourcePath = $entityPath->getAsString();
$success = $now->SetFromCurrentSystemTime();
if ($success != 1) {
    print 'Could not prepare the request.' . "\n";
    exit;
}

if ($expires->DiffSeconds($now) <= 60) {
    print 'Reacquire a token before the Table request.' . "\n";
    exit;
}

$responseText = $rest->fullRequestNoBody('GET',$resourcePath);
if ($rest->LastMethodSuccess != 1) {
    print 'Request did not complete; inspect the target before retrying a write.' . "\n";
    exit;
}

if ($rest->ResponseStatusCode != 200) {
    print 'HTTP status: ' . $rest->ResponseStatusCode . "\n";
    print 'Request ID: ' . $rest->responseHdrByName('x-ms-request-id') . "\n";
    //  404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
    //  Keep response bodies private. Reconcile uncertain writes before retrying.
    exit;
}

$success = $result->Load($responseText);
if ($success != 1) {
    print 'Expected well-formed JSON.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($result->stringOf('PartitionKey'));
$sameText7 = $textCompare->ContentsEqual('',1);
if (($result->JsonTypeOf('PartitionKey') != 1) or ($sameText7 == 1)) {
    print 'Missing string PartitionKey.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($result->stringOf('RowKey'));
$sameText8 = $textCompare->ContentsEqual('',1);
if (($result->JsonTypeOf('RowKey') != 1) or ($sameText8 == 1)) {
    print 'Missing string RowKey.' . "\n";
    exit;
}

$textCompare->Clear();
$textCompare->SetString($result->stringOf('PartitionKey'));
$sameText9 = $textCompare->ContentsEqual($partitionKey,1);
$textCompare->Clear();
$textCompare->SetString($result->stringOf('RowKey'));
$sameText10 = $textCompare->ContentsEqual($rowKey,1);
if (($sameText9 != 1) or ($sameText10 != 1)) {
    print 'Returned entity keys do not match.' . "\n";
    exit;
}

print 'Storage token obtained and Table entity retrieved.' . "\n";

?>