After initializing the KMS instance SDK client, you can use it to call the GetSecretValue API for retrieving the secret value. This topic provides code examples for this.
Complete example
Example walkthrough
Initialize client
<?php
use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;
function getDkmsGcsSdkClient()
{
global $clientKeyContent, $password, $endpoint;
// Construct the KMS instance SDK client configuration.
$config = new AlibabaCloudDkmsGcsOpenApiConfig();
// The connection protocol. Set the value to https. The KMS instance service only allows access through the HTTPS protocol.
$config->protocol = 'https';
// Client Key.
$config->clientKeyContent = $clientKeyContent;
// Client Key security token.
$config->password = $password;
// The endpoint of your KMS instance. Set the value in the following format: <ID of your KMS instance >.cryptoservice.kms.aliyuncs.com.
$config->endpoint = $endpoint;
// Instance CA certificate.
$config->caFilePath = 'path/to/caCert.pem';
// Construct the KMS instance SDK client object.
return new AlibabaCloudDkmsGcsSdkClient($config);
}Call the GetSecretValue API
function getSecretValueSample(){
global $client, $secretName;
// Construct a request to retrieve a secret value.
$getSecretValueRequest = new GetSecretValueRequest([
'secretName' => $secretName,
]);
// Ignore the server certificate.
$runtimeOptions = new RuntimeOptions();
//$runtimeOptions->ignoreSSL = true;
try {
// Call the API to retrieve a secret value.
$getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);
// Secret name.
$_secretName = $getSecretValueResponse->secretName;
// Secret value.
$_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());
}
}