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 
$public_key_res = openssl_pkey_get_public($x509_certificate_pem);
if (($error_string = openssl_error_string())!==false)
{
    error_log("OPENSSL ERR: ".$error_string);
}
else
{
    $public_key_arr = openssl_pkey_get_details($public_key_res);
    $keysize = isset($public_key_arr['bits'])? $public_key_arr['bits'] : 0;
    $key_type = 'unknown';
    $key_type = $public_key_arr['type']==OPENSSL_KEYTYPE_RSA ? 'RSA' : $key_type;
    $key_type = $public_key_arr['type']==OPENSSL_KEYTYPE_DSA ? 'DSA' : $key_type;
    $key_type = $public_key_arr['type']==OPENSSL_KEYTYPE_DH ?  'DH'  : $key_type;
    echo $key_type.' / '.$keysize ."\n";
}
?>

output:
RSA / 2048
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)