Simple Way to Parse an x509 Certificate with PHP

PHP has a nifty little function for parsing an x.509 SSL certificate into an array to easily pull out the elements: openssl_x509_parse .Essentially, all you need to do is load up the contents of the certificate, either through a file or POST value, and enclose it in the array. Here’s a simple script:

$sslcert = file_get_contents("/etc/ssl/certs/");
$sslcert = array(openssl_x509_parse($sslcert,TRUE));
foreach ($sslcert as $name => $arrays){
foreach ($arrays as $title => $value){
echo $value . "\n";
foreach($value as $subtitle => $subvalue){
echo $subtitle . " : " . $subvalue . "\n";
echo $title .  "\n";

The results are several multidimensional arrays, so depending on the data you need, you may need to keep adding foreach loops to get that data.

Be Sociable, Share!

5 thoughts on “Simple Way to Parse an x509 Certificate with PHP

Leave a Reply

Your email address will not be published. Required fields are marked *