PHP check SSL prevent php warning

2019-08-01 05:22发布

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

标签: php ssl
1条回答
放荡不羁爱自由
2楼-- · 2019-08-01 06:01

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");
}
查看更多
登录 后发表回答