【问题标题】:prevent error warnings if connection could not be established如果无法建立连接,防止出现错误警告
【发布时间】:2015-07-01 14:01:36
【问题描述】:

我有一些代码可以连接到公共网站并返回有关 SSL 证书的信息 - 代码运行良好。

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

负责建立socket连接的代码如下

进行套接字连接

$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);

如果套接字连接失败

echo "无法建立连接"

如果连接成功

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

感谢您的帮助。

【问题讨论】:

标签: php


【解决方案1】:
$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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2015-06-15
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2016-12-21
    相关资源
    最近更新 更多