Delphi';s IdHTTP.get和PHP';s file_get_contents显示同一URL的不


Delphi's IdHTTP.get and PHP's file_get_contents show different text for the same URL

我有这个链接。

我已经用IdHTTP.Get(link)(Delphi 2009)多次成功下载了它的相应数据。

这是我得到的部分结果:

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/&quot;DEIMRn47eCp7I2A9XRdXF0o.&quot;">
  <id>tag:youtube.com,2008:user:-lHJZR3Gqxm24_Vd_AJ5Yw</id>
  <published>2010-04-29T10:54:00.000Z</published>
  <updated>2014-10-31T17:29:47.000Z</updated>
  ...
</entry>

在PHP中我做:

$content = file_get_contents(link);
echo $content;

这就是我在页面上看到的确切结果:

tag:youtube.com,2008:user:-lHJZR3Gqxm24_Vd_AJ5Yw2010-04-29T10:54:00.000Z2014-10-31T17:29:47.000ZBusinessy stuff: business dot pewdiepie at gmail dot comPewDiePiehttp_gdata.youtube.com/feeds/api/users/PewDiePie-lHJZR3Gqxm24_Vd_AJ5YwUC-lHJZR3Gqxm24_Vd_AJ5Yw117663191659499528404SE-lHJZR3Gqxm24_Vd_AJ5Ywpewdiepie

PHP新手。请告诉我哪里错了。

您在PHP中得到了相同的结果,但您将其作为HTML输出到浏览器。浏览器会忽略它不知道的所有标记,并输出文本内容。在浏览器中打开源代码视图,您将看到标记。

备选方案:

输出带有转义实体的结果:

$content = file_get_contents(link);
echo htmlspecialchars($content);

以纯文本形式输出结果:

$content = file_get_contents(link);
header('Content-Type: text/plain');
echo $content;