1

I am using the PHP code below to load the certificate properties of an SSL website:

$dnsname        = 'www.google.com';
$dnsport        = 443;
$stream     = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$url        = stream_socket_client("ssl://".$dnsname.":".$dnsport, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $stream);
$content    = stream_context_get_params($url);

This all works fine when SSL is working OK. But in case no SSL connection can be opened the script results in a PHP warning:

stream_socket_client(): unable to connect to ssl://www.google.nl:443 (Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd. ) in C:\inetpub\monitor\autotask\checkssl.php on line 44

How to prevent the warning? I need a "if" but don't know what to check for.. Please help

PersianGulf
  • 2,845
  • 6
  • 47
  • 67
Mbrouwer88
  • 2,182
  • 4
  • 25
  • 38

1 Answers1

1

Suppress the warning using the @ operator and check the outcome yourself based on the return value:

$socket = @stream_socket_client(...);
if ($socket === false) {
    die("Socket error: $errstr");
}
Jon
  • 428,835
  • 81
  • 738
  • 806