PHP code example of wapacro / az-keyvault-php
1. Go to this page and download the library: Download wapacro/az-keyvault-php library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wapacro / az-keyvault-php example snippets
/**
* Secrets
*/
$secret = new AzKeyVault\Secret('https://my-keyvault-dns.vault.azure.net');
// If you want to get all secrets (default max to 25):
$secrets = $secret->getSecrets();
// ... else get next page via nextLink
$secrets = $secret->getSecrets($secrets->getNextLink());
// If you want the latest secret
$value = $secret->getSecret('mySecretName');
// If you want a specific secret version:
$value = $secret->getSecret('mySecretName', '9fe63d32-5eb0-47f2-8ef8-version-id');
// ... otherwise get all versions of secret
// with name "mySecretName" which are marked
// as enabled and retrieve the first one
$enabledSecretVersions = $secret->getSecretVersions('mySecretName')->enabled();
$firstEnabledVersion = reset($enabledSecretVersions);
$value = $secret->getSecret($firstEnabledVersion);
echo $value->secret;
// prints: my super secret message
// If you want to set secret or update secret with newer version:
$value = $secret->setSecret('mySecretName', 'mySecretValue');
echo $value->secret;
// prints: mySecretValue
/**
* Keys
*/
$key = new AzKeyVault\Key('https://my-keyvault-dns.vault.azure.net');
// Retrieve specific key version:
$value = $key->getKey('myKeyName', 'j7d8rd32-5eb0-47f2-8ef8-version-id');
// ... or get all versions of a key
// and retrieve the first one which
// is enabled, just like with the
// secrets above
$enabledKeyVersions = $key->getKeyVersions('myKeyName')->enabled();
$firstEnabledVersion = reset($enabledKeyVersions);
$value = $key->getKey($firstEnabledVersion);
echo $value->type; // e.g. "RSA"
echo $value->n; // prints base64 encoded RSA modulus
// This library also provides some key utilities
// to make retrieved keys work with the OpenSSL extension
$pem = (new AzKeyVault\KeyUtil($value))->toPEM();
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_private($pem));
var_dump($keyDetails);
composer