<?php
declare(strict_types=1);
$cases = [];
function add_case(array &$cases, string $name, mixed $expected, mixed $actual): void {
$cases[] = [
'name' => $name,
'passed' => ($actual === $expected),
'expected' => $expected,
'actual' => $actual,
];
}
$rssXml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>蓝色天空网技术订阅</title>
<link>https://example.org/tutorials</link>
<description>前端与后端开发教程更新流</description>
<item>
<title>PHP 8.4 DOM 解析实战</title>
<link>https://example.org/tutorials/php/xml-dom</link>
<description>使用 DOMDocument 解析与遍历 XML 树节点。</description>
</item>
<item>
<title>AJAX 异步交互基础</title>
<link>https://example.org/tutorials/php/ajax-intro</link>
<description>在不重载整页的前提下更新页面局部内容。</description>
</item>
<item>
<title>PDO 预处理安全指南</title>
<link>https://example.org/tutorials/php/mysql-prepared</link>
<description>使用参数绑定防止 SQL 注入。</description>
</item>
</channel>
</rss>
XML;
$tmpFile = tempnam(sys_get_temp_dir(), 'rss_demo_');
if ($tmpFile === false) {
throw new RuntimeException('Failed to create temp file');
}
$renderRss = static function (string $q, array $feedWhitelist, int $limit = 2): array {
if (!isset($feedWhitelist[$q])) {
return ['status' => 'rejected', 'html' => '', 'item_count' => 0];
}
$xmlDoc = new DOMDocument();
$xmlDoc->load($feedWhitelist[$q]);
$channel = $xmlDoc->getElementsByTagName('channel')->item(0);
$channelTitle = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$channelLink = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$channelDesc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
$channelHtml = "<p><a href='" . htmlspecialchars($channelLink, ENT_QUOTES, 'UTF-8') . "'>"
. htmlspecialchars($channelTitle, ENT_QUOTES, 'UTF-8') . "</a><br>"
. htmlspecialchars($channelDesc, ENT_QUOTES, 'UTF-8') . "</p>";
$items = $xmlDoc->getElementsByTagName('item');
$max = min($limit, $items->length);
$itemsHtml = '';
for ($i = 0; $i < $max; $i++) {
$it = $items->item($i);
$iTitle = $it->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$iLink = $it->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$iDesc = $it->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
$itemsHtml .= "<p><a href='" . htmlspecialchars($iLink, ENT_QUOTES, 'UTF-8') . "'>"
. htmlspecialchars($iTitle, ENT_QUOTES, 'UTF-8') . "</a><br>"
. htmlspecialchars($iDesc, ENT_QUOTES, 'UTF-8') . "</p>";
}
return [
'status' => 'ok',
'channel_html' => $channelHtml,
'items_html' => $itemsHtml,
'html' => $channelHtml . $itemsHtml,
'item_count' => $max,
];
};
try {
file_put_contents($tmpFile, $rssXml);
$feeds = ['rss' => $tmpFile];
// Case 1: Extract <channel> title, link, description
$res = $renderRss('rss', $feeds, 2);
add_case(
$cases,
'rss_channel_metadata_extract',
"<p><a href='https://example.org/tutorials'>蓝色天空网技术订阅</a><br>前端与后端开发教程更新流</p>",
$res['channel_html']
);
// Case 2: Bounded <item> extraction (first 2 items rendered safely)
add_case(
$cases,
'rss_items_loop_bounded',
[
'rendered_items' => 2,
'has_item1' => true,
'has_item2' => true,
'omits_item3' => true,
],
[
'rendered_items' => $res['item_count'],
'has_item1' => str_contains($res['items_html'], 'PHP 8.4 DOM 解析实战'),
'has_item2' => str_contains($res['items_html'], 'AJAX 异步交互基础'),
'omits_item3' => !str_contains($res['items_html'], 'PDO 预处理安全指南'),
]
);
// Case 3: Feed allowlist rejects arbitrary path / SSRF input
$bad = $renderRss('http://127.0.0.1/secret.xml', $feeds, 2);
add_case(
$cases,
'rss_feed_whitelist_selection',
['status' => 'rejected', 'item_count' => 0],
['status' => $bad['status'], 'item_count' => $bad['item_count']]
);
} finally {
if (is_file($tmpFile)) {
unlink($tmpFile);
}
}
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-ajax-rss-reader-lab/explore-ajax-rss-reader.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ajax-rss-reader.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1227.zip