二者都
cURL
和
file_get_contents()
可以得到
完整html输出
从url。例如,使用
file\u get\u contents()
您可以这样做:
<?php
$content = file_get_contents('http://elssolutions.co.uk/about-els');
echo $content;
但是,如果您只需要页面的一部分,
DOMDocument
和
DOMXPath
是更好的选项,对于后者,您还可以查询DOM。下面是一个示例。
<?php
// The `id` of the node in the target document to get the contents of
$url = 'http://elssolutions.co.uk/about-els';
$id = 'comp-iudvhnkb';
$dom = new DOMDocument();
// Silence `DOMDocument` errors/warnings on html5-tags
libxml_use_internal_errors(true);
// Loading content from external url
$dom->loadHTMLFile($url);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
// Querying DOM for target `id`
$xpathResultset = $xpath->query("//*[@id='$id']")->item(0);
// Getting plain html
$content = $dom->saveHTML($xpathResultset);
echo $content;