paptamas 0 Newbie Poster

Hy all.
I need to get the content of multiple pages from a single domain.
Now for each page I use an fsockopen connection, and I get the content of the page this way:

<?php
	$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
	if (!$fp) {
		echo "$errstr ($errno)<br />\n";
	} else {
		$out = "GET /page1.html HTTP/1.1\r\n";
		$out .= "Host: www.example.com\r\n";
		$out .= "Connection: Close\r\n\r\n";
		fwrite($fp, $out);
		while (!feof($fp)) {
			fgets($fp, 128);
		}
		fclose($fp);
	}
?>

My script wastes time, with reconnecting to the domain, to get the second page.
I was wondering, if is possible to use a single connection, and to get multiple pages, like this:

<?php
	$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
	if (!$fp) {
		echo "$errstr ($errno)<br />\n";
	} else {
//get page1
		$out = "GET /page1.html HTTP/1.1\r\n";
		$out .= "Host: www.example.com\r\n";
		$out .= "Connection: Close\r\n\r\n";
		fwrite($fp, $out);
		while (!feof($fp)) {
			fgets($fp, 128);
		}
//get page2
$out = "GET /page2.html HTTP/1.1\r\n";
		$out .= "Host: www.example.com\r\n";
		$out .= "Connection: Close\r\n\r\n";
		fwrite($fp, $out);
		while (!feof($fp)) {
			fgets($fp, 128);
		}
		fclose($fp);
	}
?>

But this method is returning the page1.html two times, I don't know why.

I tried to use: Connection: keep alive, or HTTP/1.0, but in this cases I didn't get anything from the server (infinite executing time of my script).

Any suggestion to solve this?

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.