简体   繁体   English

php代码以加载http URL的页面源

[英]php code to load page source of http URL

I am trying to find the PHP code that will load the source page of a URL on my screen - actually I am trying to do more but this is the first step I want to achieve in a clean reliable manner. 我正在尝试找到可在屏幕上加载URL源页面的PHP代码-实际上,我正在尝试做更多事情,但这是我希望以一种可靠的方式实现的第一步。 Most postings say this has been asked and replied several times but nothing seems to work reliably for me and most postings are old. 大多数帖子都说这已被要求并回答了几次,但对我而言似乎没有可靠的方法,并且大多数帖子都已过时。 On top of that I am very very new to PHP or any web programming. 最重要的是,我对PHP或任何Web编程都非常陌生。 Anyway I did find some codes using cURL, DOM or just direct functions that work but very sensitive to the PHP version. 无论如何,我确实找到了一些使用cURL,DOM的代码,或者只是可以使用但对PHP版本非常敏感的直接函数。 PHP 5.2, 5.3, 5.5 and 5.6 are the versions available from my hosting service. PHP 5.2、5.3、5.5和5.6是我的托管服务提供的版本。 The ones that work in some versions, load (display) the URL page itself or in a "bulleted" manner w/o the images - but nothing that looks like the html document when we do a "view page source" on any web page. 在某些版本中工作的页面会本身或以不显示图像的“项目符号”方式加载(显示)URL页面,但是当我们在任何网页上执行“查看页面源代码”时,看起来都不像html文档。 So my question is is this something not possible at all or am I missing something here? 所以我的问题是,这根本不可能吗?或者我在这里错过了什么吗? One of the DOM codes that echoes the page but not it's source and that too only in 5.2 and 5.5 is: 回显页面但不作为页面源的DOM代码之一,而且仅在5.2和5.5中是:

<?php
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://www.cisco.com/');
echo $dom->saveHTML();
?>

One of my other important need is for my PHP codes to work in 5.3 at least for now, but would like em to work in 5.2 thru 5.5 if possible. 我的另一个重要需求之一是我的PHP代码至少现在可以在5.3中运行,但是如果可能的话,他们希望em在5.2到5.5中运行。 Any pointers please? 有指针吗?

The issue is that when you echo the HTML, the browser interprets it as HTML. 问题是当您回显HTML时,浏览器会将其解释为HTML。 If you want to see it as "source", you need to either escape the HTML: 如果要将其视为“源”,则需要转义HTML:

echo htmlspecialchars($dom->saveHTML());

or set the content type to text: 或将内容类型设置为文本:

header("Content-Type:text/plain");
echo $dom->saveHTML();

You can replace the < and > with the proper HTML entities so the source will show on the screen rather than being parsed as source by the browser: 您可以用适当的HTML实体替换<> ,这样源将显示在屏幕上,而不是被浏览器解析为源:

echo str_replace('>', '&gt;', str_replace('<', '&lt;', $dom->saveHTML()));

Or echo htmlspecialchars($dom->saveHTML()); 或者echo htmlspecialchars($dom->saveHTML()); which is cleaner ... but the above at least gives you a glimpse of what htmlspecialchars is actually doing 哪个更干净...但是以上内容至少让您了解了htmlspecialchars实际在做什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM