Question

I've made a basic script to get the html page and images. But I would like to continue to stay connected while I am retrieve pictures.
- What I did with the test script was kept on asking for addresses on the web server. But It did not work.
So how do I do it or what have I done wrong?

Basic script:

function fetch( $server, $url, $port = 80, &$resp_head )
{
    if ( !$con = @fsockopen($server, $port, $error_number, $error_string, 30) ) return False;
    stream_set_timeout( $con, 8600 );

    // Reguest
    $qry_header = 'GET '.$url." HTTP/1.1\r\n";
    $qry_header .= 'Host: '.$server."\r\n";
    $qry_header .= 'User-Agent: Mihtyom; (+http://'.$_SERVER['HTTP_HOST']."/)\r\n";
    $qry_header .= "Accept: */*\r\n";           //google check the 3 "Accept"
    $qry_header .= "Accept-Language: *\r\n";
    $qry_header .= "Accept-Encoding: *\r\n";
    $qry_header .= "Connection: Close\r\n\r\n";
    fwrite( $con, $qry_header );

    $inheader = True;
    $data = '';
    while (!feof( $con ))
    {
        $line = fgets( $con );
        if (!$inheader)
        {
            $data .= $line;
            continue;
        }
        if ($line == "\n" || $line == "\r\n")
        {
            $inheader = False;
            continue;
        }
        $resp_head[] = trim($line);
    }

    fclose( $con );

    return $data;
}

Test script:

function fetch( $server, $urls, $port = 80, &$resp_head )
{
    if ( !$con = @fsockopen($server, $port, $error_number, $error_string, 30) ) return False;
    stream_set_timeout( $con, 8600 );

    $resp_head = array();
    $data = array();
    foreach ($urls as $key => $url)
    {
        $inheader = True;
        $data[$key] = '';

        // Reguest
        $qry_header = 'GET '.$url." HTTP/1.1\r\n";
        $qry_header .= 'Host: '.$server."\r\n";
        $qry_header .= 'User-Agent: Mihtyom; (+http://'.$_SERVER['HTTP_HOST']."/)\r\n";
        $qry_header .= "Accept: */*\r\n";           //google check the 3 "Accept"
        $qry_header .= "Accept-Language: *\r\n";
        $qry_header .= "Accept-Encoding: *\r\n";
        $qry_header .= "Connection: Close\r\n\r\n";
        fwrite( $con, $qry_header );

        while (!feof( $con ))
        {
            $line = fgets( $con );
            if (!$inheader)
            {
                $data[$key] .= $line;
                continue;
            }
            if ($line == "\n" || $line == "\r\n")
            {
                $inheader = False;
                continue;
            }
            $resp_head[$key][] = trim($line);
        }
    }
    fclose( $con );
    return $data;
}

New script (21-07-12 12:34):

function fetch( $server, $urls, $port = 80, &$resp_head )
{
    if ( !$con = @pfsockopen($server, $port, $error_number, $error_string, 30) ) return False;
    stream_set_timeout( $con, 8600 );

    $resp_head = array();
    $data = '';
    $i = count($urls);
    foreach ($urls as $key => $url )
    {
        $i--;

        // Reguest
        $qry_header = 'GET '.$url." HTTP/1.1\r\n";
        $qry_header .= 'Host: '.$server."\r\n";
        $qry_header .= 'User-Agent: Mihtyom; (+http://'.$_SERVER['HTTP_HOST']."/)\r\n";
        $qry_header .= "Accept: */*\r\n";
        $qry_header .= "Accept-Language: *\r\n";
        $qry_header .= "Connection: ".($i == 0 ? 'Close' : 'keep-alive')."\r\n\r\n";
        fwrite( $con, $qry_header );
    }

    while (!feof( $con ))
    {
        $line = fgets( $con );
        if ($line == "\r\n\r\n" || $line == "\n\n") break;
        $data .= $line;
    }
    fclose( $con );
    return $data;
}
Was it helpful?

Solution

You specify Connection: Close in the HTTP header, which basically tells the server: "give me this one file and then close the connection immediately". You are actually forcing the connection to close yourself.

Instead, you can specify Connection: Keep-Alive, which tells the server (explicitly) to not close the TCP session so you can reuse it for another request.

However, the server must not accept this in any case - it may ignore your request and answer with Connection: Close. Also, each side of the connection may at any time close the connection and/or tell the other side to do so in the (next) header, via Connection: Close.

For a correct implementation, you should specify Connection: Close when your loop processes the last URL.

Find more details about persistent connections in HTTP 1.1 / RFC 2068 Section 14.10, HTTP 1.1 / RFC 2616 Section 19.6.2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top