Skip to content

Instantly share code, notes, and snippets.

@dracken
Forked from brankoajzele/cerToPem.php
Created August 5, 2016 23:11
Show Gist options
  • Save dracken/94ad377ec757923529de30a91f960a33 to your computer and use it in GitHub Desktop.
Save dracken/94ad377ec757923529de30a91f960a33 to your computer and use it in GitHub Desktop.
Converting .cer to .pem via pure PHP, (no system, backticks, shell_exec, exec, etc.) to get the same result as with "openssl x509 -inform der -in cert.cer -out cert.pem". Note, I am not expert on certificates, etc. This specific certificate conversion simply worked for me.
<?php
$certificateCAcer = '/certificate.cer';
$certificateCAcerContent = file_get_contents($certificateCAcer);
/* Convert .cer to .pem, cURL uses .pem */
$certificateCApemContent = '-----BEGIN CERTIFICATE-----'.PHP_EOL
.chunk_split(base64_encode($certificateCAcerContent), 64, PHP_EOL)
.'-----END CERTIFICATE-----'.PHP_EOL;
$certificateCApem = $certificateCAcer.'.pem';
file_put_contents($certificateCApem, $certificateCApemContent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment