Effectively these method are doing the same. However, using file_get_contents()
you will need to store the results, at least temporarily, in a string variable unless you pass it to DOMDocument::loadHTML()
. This leads to a higher memory usage in your application.
Some sites may require you to set some special header values, or use an other HTTP method than GET
. If you need this, you need to specify a so called stream context. You can achieve this for both of the above methods using stream_context_create()
:
Example:
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$ctx = stream_context_create($opts);
You can set this context using both of the above ways, but they differ in how to achieve this:
// With file_get_contents ...
$file_get_contents($url, false, $ctx);
// With DOM
libxml_set_streams_context($ctx);
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
Leaves to be said, that using the curl
extension you will have even more control about he HTTP transfer, what might be necessary in some special cases.