Skip to content

Instantly share code, notes, and snippets.

@brankoajzele
Created January 21, 2013 13:13
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save brankoajzele/4585931 to your computer and use it in GitHub Desktop.
Save brankoajzele/4585931 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);
@marioruizm
Copy link

Thanks for your help!
I need to do the same but to convert file.key to "file.key.pem".. May you help me?

@PauloPeres
Copy link

Thxxx Helped me with Apple Certification

@vutrunghieu
Copy link

You saved my life,
Thank you, sir...

@RicardoCruz6395
Copy link

Muchas gracias por el aporte. Me ha servido mucho :)

@michealzh
Copy link

Thank you for your sharing

@woodyxiong
Copy link

thank you for sharing,it's very useful to me

@JulioLeijaMultifacturas
Copy link

Thanks for your code, is really useful.

@maka2004
Copy link

Thank you, mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment