Search code examples
phpdomdocument

PHP specifying timeout duration for DOMDocument?


I have a code that will scrap some data from a website using PHP DomDocument. However, I would like to specify the timeout duration. Is there any way to do so?

The code that I have is something like this:

<?php 
$grep = new DoMDocument(); 
@$grep->loadHTMLFile("http://www.example.com"); 
$finder = new DomXPath($grep); 
$class = "Cat"; 
$nodes = $finder->query("//*[contains(@class, '$class')]"); 

foreach ($nodes as $node) 
{ 

//coding    
}   
?> 

Thanks!


Solution

  • Alternatively, you could use fil_get_contents() and set there the timeout duration, then feeding the return values into the DOMDocument class:

    $context = stream_context_create(array(
        'http' => array('timeout' => 60), // 60 seconds
    ));
    $html_page = file_get_contents('http://www.example.com', false, $context);
    
    $grep = new DoMDocument(); 
    @$grep->loadHTML($html_page); 
    $finder = new DomXPath($grep); 
    $class = "Cat"; 
    $nodes = $finder->query("//*[contains(@class, '$class')]");