Search code examples
phphttpscertificatessl-certificatepem

Extract certificate from HTTPS site


I need to extract the public key (preferably in PEM format) that a site uses to identify itself with.

E.g. a function that takes a URL as an argument (https://www.example.com/), then establish a connection to that site and fetches the certificate.

Any ideas about how it could be solved with PHP code?


Solution

  • You could use openssl to extract the certificate.

    openssl s_client -connect google.com:443 > file.txt
    

    You will find the certificate in file.txt between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----, and you can extract it from there. I don't see a way to use the PHP OpenSSL module to do this from within PHP though, unfortunately.