Hi,

fsockopen and fgets is very slow compared
to file("http://...").

I want to download some pages from a website using PHP. I first thought of using 'file("http://...")'. Getting a page is rather fast (1-2 sec to load the page from a local web server that load the page from a remote website). But I realised the web site returns pages that depend on the browser.

Thus I turned to fsockopen and fgets. And now, I get the result I want, but the same page needs 15 seconds to do load !!!

here is the code:

<?
function getAsp($url)
{
if (strpos($url, "?")!=0)
$url = $url."&frame=true";
else
$url = $url."?frame=true";

$fp = fsockopen ("msdn.microsoft.com", 80, $errno, $errstr, 30);
if (!$fp) {
	echo "$errstr ($errno)<br>\n";
} else {
	$cmd = 
		"GET $url HTTP/1.0\r\n".
		"Accept-Language: es\r\n".
		"User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)\r\n".
		"Connection: Keep-Alive".
		"Accept: image/gif, image/jpeg, */*".
		"Host: Host: msdn.microsoft.com\r\n\r\n";
	fputs ($fp, $cmd);
	while (!feof($fp)) {
		fgets($fp,128);
	}
	fclose ($fp);
}

}
$chkUrl_path = "/library/en-us/cmctl198/html/vbobjtreeview.asp";
?>
<XMP>
<?
getAsp($chkUrl_path);
// $lines = file("http://msdn.microsoft.com".$chkUrl_path."?frame=true");
?>
</XMP>

    Hi,

    The fgets is slow because you have a low byte-tange '128' in fgets, change it to 1024. It may also be slow because you two 'Host:' statements.

    Elfyn

      Ive noticed fopen/fgets/fclose are very slow compared to the single file() function.

      I had a website that remotely grabbed "Ad banner" code from a remote website source php... and using the fopen (even with a byte range of a healthy 4096) would be terribly slow.

      Changing them all to a file() sped it up considerably!

      I just had to put the result in this fashion:
      $results = implode('',file("website/ads.php"));

      Which works out perfectly when spitting $results into a spot in a webpage later.

        Write a Reply...