views:

25

answers:

1

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 {

    $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);
    } $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!

+2  A: 

Try only sending the Connection: Close header on the last request.

EDIT: Clarification

$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";
    // DON'T SEND Connection: Close HERE
    fwrite($fp, $out);
    while (!feof($fp)) {
        fgets($fp, 128);
    } 

    $out = "GET /page2.html HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    // THIS IS THE LAST PAGE REQUIRED SO SEND Connection: Close HEADER
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        fgets($fp, 128);
    }
    fclose($fp);
}
rojoca
If I send just Connection: Close, how will the server know that I need tha page2.html to be retrieved?
Tamas Pap
You still send all the other headers for each page but only send `Connection: Close` for the last page.
rojoca
Infinite loading time..in this case..Thank you anyway;)
Tamas Pap
I think you need to send all the requests though first before you start reading the response. According to the HTTP 1.1 spec the responses should all return in the correct order but you will have to manually separate them
rojoca