Getting SSL certificate with Zend Http request and response

I am trying to connect to a 3rd party API and I am using Zend 1.7.0 framework to access it. For authentication, I must first check the SSL certificate returned from the url. This is what I am doing in my code.

$client = new Zend_Http_Client($url);
$response = $client->request('GET');

      

The answer is always ok and when I go to '$ url' I can see the certificate if I click on the lock in the bottom right corner of my browser window.

Is there a way to access the certificate owner, expiration date, and other properties through the response object? Do I have to do anything special for the request before submitting it for certificate information?

+2


source to share


1 answer


I don't think this is possible with the standard connection adapter that Zend_Http_Client uses. I did a bit of work and it looks like the adapter is using fsockopen, which pretty much hides what you are looking for. The way to do it is to open the socket yourself and get the certificate first:



$url = 'mail.google.com'; // For example
$context = stream_context_create();
$res = stream_context_set_option($context, 'ssl', 'capture_peer_cert', true);
$res = stream_context_set_option($context, 'ssl', 'verify_host', true);
if ($socket = stream_socket_client("tls://$url:443/", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context)) {
    if ($options = stream_context_get_options($context)) {
        if (isset($options['ssl']) && isset($options['ssl']['peer_certificate'])) {
            $keyinfo = openssl_x509_parse($options[$wrapper]['peer_certificate']);
            var_dump($keyinfo);
        }
    }
    fclose($fp);
}

      

+2


source







All Articles