Setelah menginisialisasi SDK client instance KMS, Anda dapat menggunakannya untuk memanggil API GetSecretValue guna mengambil nilai rahasia. Topik ini menyediakan contoh kode terkait.
Contoh lengkap
Penjelasan contoh
Inisialisasi 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;
// Buat konfigurasi SDK client instance KMS.
$config = new AlibabaCloudDkmsGcsOpenApiConfig();
// Protokol koneksi. Setel nilainya ke https. Layanan instance KMS hanya mengizinkan akses melalui protokol HTTPS.
$config->protocol = 'https';
// Client Key.
$config->clientKeyContent = $clientKeyContent;
// Token keamanan Client Key.
$config->password = $password;
// Endpoint instance KMS Anda. Setel nilainya dalam format berikut: <ID instance KMS Anda>.cryptoservice.kms.aliyuncs.com.
$config->endpoint = $endpoint;
// Sertifikat CA instance.
$config->caFilePath = 'path/to/caCert.pem';
// Buat objek SDK client instance KMS.
return new AlibabaCloudDkmsGcsSdkClient($config);
}Panggil API GetSecretValue
function getSecretValueSample(){
global $client, $secretName;
// Buat permintaan untuk mengambil nilai rahasia.
$getSecretValueRequest = new GetSecretValueRequest([
'secretName' => $secretName,
]);
// Abaikan sertifikat server.
$runtimeOptions = new RuntimeOptions();
//$runtimeOptions->ignoreSSL = true;
try {
// Panggil API untuk mengambil nilai rahasia.
$getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);
// Nama rahasia.
$_secretName = $getSecretValueResponse->secretName;
// Nilai rahasia.
$_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());
}
}