PHP指定DOMDocument的超时持续时间?

PHP指定DOMDocument的超时持续时间?,php,domdocument,Php,Domdocument,我有一个代码,可以使用PHP文档从网站上删除一些数据。但是,我想指定超时持续时间。有什么办法吗 我的代码是这样的: <?php $grep = new DoMDocument(); @$grep->loadHTMLFile("http://www.example.com"); $finder = new DomXPath($grep); $class = "Cat"; $nodes = $finder->query("//*[contains(@class, '$cl

我有一个代码,可以使用PHP文档从网站上删除一些数据。但是,我想指定超时持续时间。有什么办法吗

我的代码是这样的:

<?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    
}   
?> 

谢谢

或者,您可以使用fil_get_contents并在那里设置超时持续时间,然后将返回值输入到DOMDocument类中:

$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')]"); 
该函数可用于为下一次加载或写入libxml文档设置streams上下文

另见其他

$opts = [
    'http' => [
        'timeout' => 10, // 10 seconds
    ]
];

$context = stream_context_create($opts);
libxml_set_streams_context($context);

$grep = new DoMDocument(); 
$grep->loadHTMLFile("https://www.example.com");