初始化KMS实例SDK客户端后,您可以通过客户端调用GetSecretValue接口获取凭据值。本文介绍获取凭据值的代码示例。
完整代码示例
调用接口获取凭据值。
源码github地址:GetSecretValue.php
代码示例解析
初始化客户端
关于初始化客户端的详细介绍,请参见初始化客户端。
<?php
use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;
function getDkmsGcsSdkClient()
{
    global $clientKeyContent, $password, $endpoint;
    // 构建KMS实例SDK Client配置
    $config = new AlibabaCloudDkmsGcsOpenApiConfig();
    //连接协议请设置为"https"。KMS实例服务仅允许通过HTTPS协议访问。
    $config->protocol = 'https';
    //Client Key。
    $config->clientKeyContent = $clientKeyContent;
    //Client Key口令。
    $config->password = $password;
    //设置endpoint为<your KMS Instance Id>.cryptoservice.kms.aliyuncs.com。
    $config->endpoint = $endpoint;
    // 实例CA证书
    $config->caFilePath = 'path/to/caCert.pem';
    // 构建KMS实例SDK Client对象
    return new AlibabaCloudDkmsGcsSdkClient($config);
}调用GetSecretValue接口获取凭据值
function getSecretValueSample(){
    global $client, $secretName;
    // 构建获取凭据请求
    $getSecretValueRequest = new GetSecretValueRequest([
        'secretName' => $secretName,
    ]);
    // 忽略服务端证书
    $runtimeOptions = new RuntimeOptions();
    //$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());
    }
}该文章对您有帮助吗?