Jump to content

High CPU load on simple_html_dom


etrader

Recommended Posts

I successfully load a page by simple_html_dom.php (developed in simplehtmldom.sourceforge.net) as

$html = file_get_html('externalpage');

 

But sometimes this make a high load on CPU and the page does not load for a long time (probably due to the external site server). How can I skip the process when it is not normal to avoid high CPU usage?

Link to comment
https://forums.phpfreaks.com/topic/227913-high-cpu-load-on-simple_html_dom/
Share on other sites

download the file first?

 

(untested)

$cachepage = "cache/pagename.html";
$external = "http://google.com";
if(!file_exists($cachepage)) {
	$ch = curl_init($external);
	$fp = fopen($cachepage,'w');
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_FILE, $fp);
	curl_exec($ch);
	curl_close($ch);
	fclose($fp);
}
$html = file_get_html($cachepage);
unset($cachepage);

Just throwing it out there, but maybe using one of the half dozen built-in DOM libraries in PHP instead of one thrown together with a bunch of regular expressions and recursive calls would improve performance?

 

What do you mean by that? I did not get it  ::)

PHP already has a number of XML libraries that do all the functionality that it looks like simplehtmldom is trying to replicate, which is probably why that project seems abandoned (last commit was 2008).

 

http://us2.php.net/manual/en/refs.xml.php

 

Not sure what you're trying to do, but DOM and SimpleXML come to mind as good ones to look at.

download the file first?

 

(untested)

$cachepage = "cache/pagename.html";
$external = "http://google.com";
if(!file_exists($cachepage)) {
	$ch = curl_init($external);
	$fp = fopen($cachepage,'w');
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_FILE, $fp);
	curl_exec($ch);
	curl_close($ch);
	fclose($fp);
}
$html = file_get_html($cachepage);
unset($cachepage);

 

The only problem I have is that it writes pagename.html with chmod 644, and then unset cannot delete it or re-writing in the next run.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.