开发者

Get multiple pages with a single fsockopen

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开发者_如何学JAVA";
    $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!


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜