如果无法建立连接,则防止出现错误警告

时间:2015-04-23 11:47:11

标签: php

我有一些代码可以连接到公共网站并返回有关SSL证书的信息 - 代码工作正常。

我想添加一个if语句,如果连接无法建立,那么它将返回"无法建立连接" ..现在,如果它无法连接它会产生一大堆警告

负责建立套接字连接的代码如下

进行插座连接

$ctx = stream_context_create( array("ssl" => $ssloptions) );
$result = stream_socket_client("ssl://$url:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
$cont = stream_context_get_params($result);

如果SOCKET连接失败

  

echo"无法建立连接"

如果连接成功

foreach($cont["options"]["ssl"]["peer_certificate_chain"] as $cert) {
    openssl_x509_export($cert, $pem_encoded);
    echo $pem_encoded;
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

$ctx = @stream_context_create( array("ssl" => $ssloptions) );
$result = @stream_socket_client("ssl://$url:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);

if($result == false) {
    echo "could not establish connection";
} else {
    $cont = @stream_context_get_params($result);
    foreach($cont["options"]["ssl"]["peer_certificate_chain"] as $cert) {
        openssl_x509_export($cert, $pem_encoded);
        echo $pem_encoded;
    }
}