如果您遇到了“API请求 - 无法加载PEM证书?OpenSSL错误,HTTR R,CURL”错误,可能是由于您的证书格式不正确导致的。您可以简单地将PEM格式的证书转换为DER格式,然后重新加载它。
以下是使用PHP的示例代码:
// 将路径更改为您的证书路径
$pem_file = "path/to/your/certificate.pem";
// 将PEM格式证书转换为DER格式
$der_file = tempnam(sys_get_temp_dir(), 'php');
if (!file_put_contents($der_file, file_get_contents($pem_file))) {
die("Failed to write DER file");
}
// 使用DER格式证书进行API请求
$request_url = "https://api.example.com";
$request_options = array(
"sslcerttype" => "DER",
"sslcert" => $der_file,
"sslkeytype" => "DER",
"sslkey" => $der_file
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $request_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));
curl_setopt_array($ch, $request_options);
$response = curl_exec($ch);
if (curl_errno($ch)) {
die("API Request Failed: " . curl_error($ch));
}
curl_close($ch);
// cleanup
unlink($der_file);
这段代码将使用PHP的cURL库来进行API请求,并使用上述步骤转换的DER格式证书进行SSL认证。另请注意,此示例代码中的SSL验证选项被禁用。这是因为大多数API端点使用自签名证书,而不是由受信任的CA颁发的证书。如果API端点使用受信任的CA颁发的证书,则应启用SSL验证。