PHP Fetch SSL Certificate



<?php
$hostname = "blackboard.boisestate.edu";
$ssloptions = array(
    "capture_peer_cert" => true, 
    "allow_self_signed"=>false, 
    "CN_match"=>$hostname, 
    "verify_peer"=>true, 
    "SNI_enabled"=>true,
    "SNI_server_name"=>$hostname,
    "cafile"=>'/etc/ssl/certs/ca-certificates.crt' //mozilla ca cert bundle: http://curl.haxx.se/docs/caextract.html
);
 
$ctx = stream_context_create( array("ssl" => $ssloptions) );
$result = stream_socket_client("ssl://$hostname:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
$cont = stream_context_get_params($result);
openssl_x509_export($cont["options"]["ssl"]["peer_certificate"], $pem_encoded);
//$cont["options"]["ssl"]["peer_certificate_chain"]
echo $pem_encoded;
?>

output:
-----BEGIN CERTIFICATE-----
MIIGYzCCBUugAwIBAgIQDQsHQ9qITxKbaYyXhi27TTANBgkqhkiG9w0BAQUFADBm
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSUwIwYDVQQDExxEaWdpQ2VydCBIaWdoIEFzc3VyYW5j
ZSBDQS0zMB4XDTEwMDEwNDAwMDAwMFoXDTEzMDUwNTIzNTk1OVowgZQxCzAJBgNV
BAYTAlVTMQ4wDAYDVQQIEwVJZGFobzEOMAwGA1UEBxMFQm9pc2UxHzAdBgNVBAoT
FkJvaXNlIFN0YXRlIFVuaXZlcnNpdHkxKTAnBgNVBAsTIE9mZmljZSBvZiBJbmZv
cm1hdGlvbiBUZWNobm9sb2d5MRkwFwYDVQQDFBAqLmJvaXNlc3RhdGUuZWR1MIGf
MA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZap/3GD5vu8rEemolAyWonrmZ2Nax
Y0FdGMgtwZWOqXEEY3lceuu8nzmmZjeTzTTHtyVTL2wawc7p6Hv36HgQP7WoL9eS
D5fZ2U0Xuv56o3gEki8qFAIXKcjME2T2rwGdK0/HAenf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