php 8.6的stream_socket_get_crypto_status()能够报告非阻塞tls握手当前需要等待读取还是写入。stream_socket_enable_crypto()返回0表示仍在握手时,应按want_read或want_write交给stream_select()等待,并设置总截止时间;不能把0当失败,也不能在循环里关闭证书验证来绕过错误。

PHP8.6新增函数列表中的TLS状态查询接口
固定目的地,并始终校验证书
<?php if (!function_exists('stream_socket_get_crypto_status')) {
throw new RuntimeException('需要PHP 8.6 OpenSSL流支持');
}
$host = 'www.php.net';
$ctx = stream_context_create(['ssl' => [
'verify_peer' => true, 'verify_peer_name' => true, 'peer_name' => $host,
]]);
$stream = stream_socket_client('tcp://' . $host . ':443', $errno, $error,
5, STREAM_CLIENT_CONNECT, $ctx);
if ($stream === false) throw new RuntimeException('TCP连接失败');
try {
stream_set_blocking($stream, false);
$deadline = hrtime(true) + 5_000_000_000;
do {
$ok = stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if ($ok === true) break;
if ($ok === false) throw new RuntimeException('TLS握手失败');
$remaining = $deadline - hrtime(true);
if ($remaining <p>这个示例的TCP连接采用最长5秒的阻塞连接阶段,随后才进行最长约5秒的非阻塞TLS握手,两段时间应分别理解。目标为代码固定域名;若改成外部输入,还需要目的地主机、端口和解析地址的访问策略,不能直接开放服务器代理请求。</p><p style="text-align:center">@@##@@</p><p style="text-align:center">官方握手测试区分WANT_READ与WANT_WRITE,完成后状态重置为NONE</p><h2>握手结束后仍要管理应用层I/O</h2><p>正常完成仅表示TLS握手通过,不代表已经发送HTTP请求或收到业务正文。后续非阻塞读写仍需处理短写、空读、连接关闭和自己的截止时间;状态查询也不是“网页是否健康”的检测接口。CA证书缺失、主机名不匹配或协议配置不兼容时,应修复相应环境,而不是设置verify_peer=false。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>

php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










