Skip to content

Instantly share code, notes, and snippets.

@arcanoix
Forked from brankoajzele/cerToPem.php
Created March 24, 2021 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arcanoix/e1581087e448df558cac136cfc2336ef to your computer and use it in GitHub Desktop.
Save arcanoix/e1581087e448df558cac136cfc2336ef 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