<?php
declare(strict_types=1);
$cases = [];
// Case 1: Core HTTP functions availability & headers_sent()/headers_list() in CLI
$file = '';
$line = 0;
$sent = headers_sent($file, $line);
$list = headers_list();
$actual1 = [
'sapi' => PHP_SAPI,
'all_five_exist' => function_exists('header')
&& function_exists('headers_list')
&& function_exists('headers_sent')
&& function_exists('setcookie')
&& function_exists('setrawcookie'),
'headers_sent_cli' => $sent,
'headers_list_cli' => $list,
];
$expected1 = [
'sapi' => 'cli',
'all_five_exist' => true,
'headers_sent_cli' => false,
'headers_list_cli' => [],
];
$cases[] = [
'name' => 'headers_sent_and_list_in_cli',
'passed' => $actual1 === $expected1,
'expected' => $expected1,
'actual' => $actual1,
];
// Case 2: http_build_query(), parse_url(), rawurlencode()
$query = http_build_query(['q' => 'php http', 'page' => 2], '', '&', PHP_QUERY_RFC3986);
$url = 'https://www.example.com/search?' . $query;
$parsed = parse_url($url);
$raw = rawurlencode('hello world+1');
$form = urlencode('hello world+1');
$actual2 = [
'query' => $query,
'host' => $parsed['host'],
'path' => $parsed['path'],
'rawurlencode' => $raw,
'urlencode' => $form,
];
$expected2 = [
'query' => 'q=php%20http&page=2',
'host' => 'www.example.com',
'path' => '/search',
'rawurlencode' => 'hello%20world%2B1',
'urlencode' => 'hello+world%2B1',
];
$cases[] = [
'name' => 'http_query_and_url_encoding',
'passed' => $actual2 === $expected2,
'expected' => $expected2,
'actual' => $actual2,
];
// Case 3: Set-Cookie encoding contract (setcookie vs setrawcookie) & header CRLF guard
$cookieVal = 'user name=a+b';
$encodedHeader = 'Set-Cookie: prefs=' . urlencode($cookieVal) . '; Path=/; HttpOnly; SameSite=Lax';
$isValidRawCookie = static fn(string $v): bool => !preg_match('/[\x00-\x20,;\\\\"\r\n]/', $v);
$isSafeHeaderLine = static fn(string $h): bool => !preg_match('/[\r\n]/', $h);
$actual3 = [
'encoded_cookie_header' => $encodedHeader,
'raw_safe_token' => $isValidRawCookie('tok_2026_abc'),
'raw_rejects_semicolon' => !$isValidRawCookie('a;b'),
'header_rejects_crlf' => !$isSafeHeaderLine("Location: /ok\r\nSet-Cookie: evil=1"),
];
$expected3 = [
'encoded_cookie_header' => 'Set-Cookie: prefs=user+name%3Da%2Bb; Path=/; HttpOnly; SameSite=Lax',
'raw_safe_token' => true,
'raw_rejects_semicolon' => true,
'header_rejects_crlf' => true,
];
$cases[] = [
'name' => 'cookie_and_header_contract_guard',
'passed' => $actual3 === $expected3,
'expected' => $expected3,
'actual' => $actual3,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-ref-http-lab/explore-ref-http.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-http.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1293.zip