PHP file_get_contents() 显示页面而不是 HTML 源代码


php file_get_contents() shows page instead of html source code

我有这段代码,它应该获取网站的源代码。

$homepage = file_get_contents('http://homepage.com');
echo $homepage;

而不是真正给我源代码..它向我显示了我尝试从中获取源代码的页面。

使用htmlentities或更改内容类型。

$homepage = file_get_contents('http://homepage.com');
echo htmlentities($homepage);

header('Content-type: text/plain');
$homepage = file_get_contents('http://homepage.com/');
echo $homepage;

试试这个,使用 htmlspecialchars

$homepage = file_get_contents('http://homepage.com');
echo htmlspecialchars($homepage);

这是因为您正在获取源代码并(重新)输出它。您的页面只是镜像http://homepage.com

要查看实际的页面源代码,请在 echo 语句之前添加 Content-Type 标头:

header('Content-type: text/plain');

这告诉浏览器将源视为纯文本,而不是将其解释为 HTML。

这是因为您正在打印源代码,您的浏览器会将其解释为网页。要查看实际代码使用情况,请执行以下操作:

echo htmlspecialchars($homepage);