Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.