acme-client-php 写一个示例
时间: 2024-06-09 21:11:34 浏览: 15
好的,下面是一个使用 acme-client-php 库实现 ACME 协议自动签发证书的示例代码:
```php
use AcmePhp\Core\AcmeClient;
use AcmePhp\Core\Protocol\Challenge;
use AcmePhp\Ssl\Certificate;
use AcmePhp\Ssl\CertificateRequest;
use AcmePhp\Ssl\PrivateKey;
use AcmePhp\Ssl\Signer\CertificateRequestSigner;
use AcmePhp\Ssl\Signer\DataSigner;
use AcmePhp\Ssl\Signer\SignerFactory;
use AcmePhp\Ssl\Generator\KeyPairGenerator;
use AcmePhp\Ssl\Parser\KeyParser;
use AcmePhp\Ssl\Parser\CertificateParser;
use AcmePhp\Ssl\Parser\CertificateRequestParser;
// ACME 服务端的 API 地址
$serverUrl = 'https://acme-v02.api.letsencrypt.org/directory';
// 要签发证书的域名列表
$domains = ['example.com', 'www.example.com'];
// 创建一个用于存储证书和私钥的目录
$storagePath = '/path/to/storage';
if (!file_exists($storagePath)) {
mkdir($storagePath, 0777, true);
}
// 创建一个私钥
$keyPairGenerator = new KeyPairGenerator();
$privateKey = $keyPairGenerator->generatePrivateKey();
// 创建 ACME 客户端
$acmeClient = new AcmeClient($serverUrl, $privateKey, $storagePath);
// 获取 ACME 服务端提供的签发证书所需的验证数据
$authorizationChallenges = $acmeClient->requestAuthorizationChallenges($domains);
// 在域名的 DNS 记录中添加验证数据
foreach ($authorizationChallenges as $authorizationChallenge) {
if ($authorizationChallenge->getType() === Challenge::TYPE_DNS) {
$dnsRecordName = '_acme-challenge.' . $authorizationChallenge->getDomain();
$dnsRecordValue = $authorizationChallenge->getPayload();
// TODO: 将 $dnsRecordName 和 $dnsRecordValue 写入 DNS 记录
}
}
// 等待 DNS 记录生效
sleep(30);
// 向 ACME 服务端验证验证数据
$acmeClient->verifyAuthorizationChallenges($authorizationChallenges);
// 生成证书请求文件
$certificateRequest = new CertificateRequest($domains, $privateKey);
$certificateRequestSigner = new CertificateRequestSigner(new DataSigner(SignerFactory::create()), new KeyParser());
$certificateRequestPem = $certificateRequestSigner->sign($certificateRequest)->getPEM();
// 向 ACME 服务端申请证书
$certificatePem = $acmeClient->requestCertificate($certificateRequestPem);
// 解析证书和私钥
$certificateParser = new CertificateParser();
$certificate = $certificateParser->parse($certificatePem);
$privateKeyParser = new KeyParser();
$privateKey = $privateKeyParser->parse($privateKey->toPem());
// 将证书和私钥保存到文件中
$certificatePath = $storagePath . '/certificate.pem';
$privateKeyPath = $storagePath . '/private-key.pem';
file_put_contents($certificatePath, $certificate->toPem());
file_put_contents($privateKeyPath, $privateKey->toPem());
// 打印证书信息
print('Certificate:\n');
print($certificate->toPem());
print("\n");
// 打印私钥信息
print('Private key:\n');
print($privateKey->toPem());
print("\n");
```
注意,这只是一个示例代码,需要根据自己的实际情况进行修改。特别是 DNS 记录的操作需要根据自己的 DNS 服务商进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)