兼容 PHP 8.5:消除全部弃用调用

PHP 8.5 把 curl_close()/finfo_close() 这类"空操作函数"标为弃用,
并把预定义局部变量 http_response_header 标为弃用(编译期判定,@ 与三元短路都压不住)。

- 句柄关闭收进 fb_curl_close()/fb_finfo_close():仅 PHP < 8.0 真正调用,
  新版本直接跳过(8.0 起句柄随引用计数释放),保留 7.4 兼容
- 响应头读取改用 http_get_last_response_headers()(8.4+);
  老版本经 get_defined_vars() 从作用域变量表取,源码不再出现已弃用标识符
- 新 API 是全局状态,用「请求前基线」比对,避免把上一次的残留头当成本次结果
- 安装体检与 README 的推荐版本区间更新为 8.1~8.5

验证:PHP 8.5.12 下五套测试全绿(25/25、22/22、60/60、7/7、反向 4 组全被抓住),
error_log 零条弃用记录;PHP 8.4.25 同样全绿。
This commit is contained in:
2026-10-23 08:23:55 +08:00
parent 5006d54be3
commit 6135e7b890
6 changed files with 80 additions and 11 deletions
+17 -7
View File
@@ -490,7 +490,7 @@ function fb_oss_put_file($key, $path, $mime = 'application/octet-stream') {
$errno = curl_errno($ch);
$errtxt = curl_error($ch);
$status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fb_curl_close($ch);
if ($errno) { @fclose($fp); return [false, '上传请求失败:' . $errtxt, $status]; }
$errBody = (string)$body;
} else {
@@ -502,9 +502,15 @@ function fb_oss_put_file($key, $path, $mime = 'application/octet-stream') {
'timeout' => $timeout,
'ignore_errors' => true,
]]);
$hdrBefore = FB_HAS_HTTP_LAST_HDRS ? http_get_last_response_headers() : null;
$body = @file_get_contents($url, false, $ctx);
if (isset($http_response_header) && is_array($http_response_header)
&& preg_match('#^HTTP/\S+\s+(\d{3})#', $http_response_header[0], $m)) {
// PHP 8.5 起预定义局部变量 http_response_header 已弃用(连 @ 都压不住),
// 8.4 起改用 http_get_last_response_headers();老版本仍读局部变量。
// 三元短路:新版本不执行第二支,老版本也不碰第一支。
$hdrRaw = FB_HAS_HTTP_LAST_HDRS
? fb_last_http_headers($hdrBefore)
: fb_local_http_headers(get_defined_vars());
if ($hdrRaw && preg_match('#^HTTP/\S+\s+(\d{3})#', $hdrRaw[0], $m)) {
$status = (int)$m[1];
}
$errBody = ($body === false ? '' : (string)$body);
@@ -588,7 +594,7 @@ function fb_oss_stream_get($key, $rangeHeader = '', array $subResource = [], $on
$errno = curl_errno($ch);
$errtxt = curl_error($ch);
$st2 = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fb_curl_close($ch);
if ($st2 > 0) { $status = $st2; }
if (!$statusFired && is_callable($onStatus)) { $onStatus($status); }
@@ -685,7 +691,7 @@ function fb_http($method, $url, array $opt = []) {
$bad = ($raw === false) || curl_errno($ch);
$hsz = (int)curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$st = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fb_curl_close($ch);
if ($bad) { return [0, [], '']; }
return [$st, fb_parse_headers(substr((string)$raw, 0, $hsz)), (string)substr((string)$raw, $hsz)];
}
@@ -698,12 +704,16 @@ function fb_http($method, $url, array $opt = []) {
];
if ($noproxy) { $streamOpt['proxy'] = ''; $streamOpt['request_fulluri'] = false; }
$ctx = stream_context_create(['http' => $streamOpt]);
$hdrBefore = FB_HAS_HTTP_LAST_HDRS ? http_get_last_response_headers() : null;
$body = @file_get_contents($url, false, $ctx);
$status = 0; $rawHead = '';
if (isset($http_response_header) && is_array($http_response_header)) {
$hdrRaw = FB_HAS_HTTP_LAST_HDRS
? fb_last_http_headers($hdrBefore)
: fb_local_http_headers(get_defined_vars());
if ($hdrRaw) {
$lines = [];
foreach ($http_response_header as $line) {
foreach ($hdrRaw as $line) {
if (preg_match('#^HTTP/\S+\s+(\d{3})#i', $line, $m)) { $status = (int)$m[1]; $lines = []; }
else { $lines[] = $line; }
}