Retrieves a secret value from a Key Management Service (KMS) instance by calling the GetSecretValue API. The following PHP examples show a complete runnable script and a step-by-step walkthrough.
Prerequisites
Before you begin, ensure that you have:
A KMS instance with the SDK client initialized. See Initialize client.
A ClientKey file obtained from KMS application management, with its encryption password stored as the
CLIENT_KEY_PASSWORDenvironment variable.A secret (credential) created in your KMS instance.
Complete example
Example walkthrough
Initialize client
The getDkmsGcsSdkClient() function builds and returns the KMS instance SDK client. It constructs a Config object with the ClientKey content, password, endpoint, and CA certificate path. The protocol must be set to https.
function getDkmsGcsSdkClient() {
global $clientKeyContent, $password, $endpoint;
$config = new AlibabaCloudDkmsGcsOpenApiConfig();
$config->protocol = 'https';
$config->clientKeyContent = $clientKeyContent;
$config->password = $password;
// Format: <KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com
$config->endpoint = $endpoint;
$config->caFilePath = 'path/to/caCert.pem';
return new AlibabaCloudDkmsGcsSdkClient($config);
}Call the GetSecretValue API
The getSecretValueSample() function calls getSecretValueWithOptions() with the secret name and runtime options. The response contains secretName and secretData fields.
function getSecretValueSample() {
global $client, $secretName;
$getSecretValueRequest = new GetSecretValueRequest([
'secretName' => $secretName,
]);
$runtimeOptions = new RuntimeOptions();
// Uncomment the following line to skip server certificate verification.
// $runtimeOptions->ignoreSSL = true;
try {
$getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);
$_secretName = $getSecretValueResponse->secretName;
$_secretData = $getSecretValueResponse->secretData;
var_dump($getSecretValueResponse->toMap());
} catch (\Exception $error) {
if ($error instanceof \AlibabaCloud\Tea\Exception\TeaError) {
var_dump($error->getErrorInfo());
}
var_dump($error->getMessage());
var_dump($error->getTraceAsString());
}
}