<?php
declare(strict_types=1);
$cases = [];
// Case 1: libxml version & error level constants
$actual1 = [
'ext_libxml_loaded' => extension_loaded('libxml'),
'libxml_version' => LIBXML_VERSION,
'libxml_dotted_version' => LIBXML_DOTTED_VERSION,
'err_levels' => [
'none' => LIBXML_ERR_NONE,
'warning' => LIBXML_ERR_WARNING,
'error' => LIBXML_ERR_ERROR,
'fatal' => LIBXML_ERR_FATAL,
],
];
$expected1 = [
'ext_libxml_loaded' => true,
'libxml_version' => 21109,
'libxml_dotted_version' => '2.11.9',
'err_levels' => [
'none' => 0,
'warning' => 1,
'error' => 2,
'fatal' => 3,
],
];
$cases[] = [
'name' => 'libxml_version_and_constants',
'passed' => $actual1 === $expected1,
'expected' => $expected1,
'actual' => $actual1,
];
// Case 2: libxml_use_internal_errors(), libxml_get_errors(), libxml_get_last_error(), libxml_clear_errors()
$prev = libxml_use_internal_errors(true);
libxml_clear_errors();
$badDoc = simplexml_load_string('<root><item>unclosed</root>');
$errors = libxml_get_errors();
$lastErr = libxml_get_last_error();
$firstCode = $errors[0]->code ?? null;
$firstLevel = $errors[0]->level ?? null;
$lastMatches = ($lastErr instanceof LibXMLError) && ($lastErr->code === end($errors)->code);
libxml_clear_errors();
$afterClearCount = count(libxml_get_errors());
libxml_use_internal_errors($prev);
$actual2 = [
'bad_parse_returned_false' => $badDoc === false,
'error_count_positive' => count($errors) > 0,
'first_error_code' => $firstCode,
'first_error_is_fatal' => $firstLevel === LIBXML_ERR_FATAL,
'last_error_matches' => $lastMatches,
'after_clear_count' => $afterClearCount,
];
$expected2 = [
'bad_parse_returned_false' => true,
'error_count_positive' => true,
'first_error_code' => 76,
'first_error_is_fatal' => true,
'last_error_matches' => true,
'after_clear_count' => 0,
];
$cases[] = [
'name' => 'libxml_internal_errors_capture_and_clear',
'passed' => $actual2 === $expected2,
'expected' => $expected2,
'actual' => $actual2,
];
// Case 3: LIBXML_NOCDATA, LIBXML_NOBLANKS, LIBXML_NONET, LIBXML_NOEMPTYTAG, LIBXML_NOXMLDECL, libxml_set_streams_context
$ctx = stream_context_create(['http' => ['timeout' => 5]]);
libxml_set_streams_context($ctx);
$xml = simplexml_load_string(
'<doc> <note><![CDATA[A & B]]></note> </doc>',
'SimpleXMLElement',
LIBXML_NOCDATA | LIBXML_NOBLANKS | LIBXML_NONET
);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXML('<box><br/></box>', LIBXML_NONET);
$expanded = $dom->saveXML($dom->documentElement, LIBXML_NOEMPTYTAG);
$noDecl = trim($dom->saveXML(null, LIBXML_NOXMLDECL));
$actual3 = [
'cdata_text' => (string) $xml->note,
'expanded_empty_tag' => $expanded,
'no_xml_decl' => $noDecl,
];
$expected3 = [
'cdata_text' => 'A & B',
'expanded_empty_tag' => '<box><br></br></box>',
'no_xml_decl' => '<box><br/></box>',
];
$cases[] = [
'name' => 'libxml_options_nocdata_and_noemptytag',
'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-libxml-lab/explore-ref-libxml.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-libxml.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1294.zip