12
$f = fsockopen("www....",80,$x,$y);

fwrite("GET request HTTP/1.1\r\nConnection: keep-alive\r\n\r\n");

while($s = fread($f,1024)){
    ...
}

上面的档位是因为Connection: keep-alive, 和Connection: close.

你如何做到不停滞不前?

4

2 回答 2

18

这取决于响应,如果transfer-encoding响应是chunked,那么您会一直阅读,直到遇到“最后一个块”(\r\n0\r\n)。

如果content-encodinggzip,那么您查看content-length响应标头并读取那么多数据,然后对其进行膨胀。如果transfer-encoding也设置为分块,则必须对解码的响应进行分块。

最简单的事情是构建一个简单的状态机来从套接字读取响应,同时仍有数据留给响应。

读取分块数据时,您应该读取第一个块长度(以及任何分块扩展),然后读取与块大小一样多的数据,直到最后一个块为止。

换一种方式:

  • 读取 HTTP 响应头(读取小块数据,直到遇到\r\n\r\n
  • 将响应标头解析为数组
  • 如果transfer-encoding是分块的,则逐块读取和解块数据。
  • 如果content-length设置了标头,您可以从套接字读取那么多数据
  • 如果content-encoding是gzip,解压读取的数据

完成上述步骤后,您应该已经阅读了整个响应,您现在可以在同一个套接字上发送另一个 HTTP 请求并重复该过程。

另一方面,除非您绝对需要保持连接,否则只需Connection: close在请求中设置,您就可以安全地读取while (!feof($f)).

目前我没有任何用于读取和解析 HTTP 响应的 PHP 代码(我只使用 cURL),但如果您想查看实际代码,请告诉我,我可以解决问题。我还可以向您推荐我编写的一些 C# 代码,这些代码可以完成上述所有操作。

编辑:这是fsockopen用于发出 HTTP 请求并演示读取保持活动连接的工作代码,并可能使用分块编码和 gzip 压缩。经过测试,但没有受到折磨 - 使用风险自负!!!

<?php

/**
 * PHP HTTP request demo
 * Makes HTTP requests using PHP and fsockopen
 * Supports chunked transfer encoding, gzip compression, and keep-alive
 *
 * @author drew010 <http://stackoverflow.com/questions/11125463/if-connection-is-keep-alive-how-to-read-until-end-of-stream-php/11812536#11812536>
 * @date 2012-08-05
 * Public domain
 *
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

$host = 'www.kernel.org';

$sock = fsockopen($host, 80, $errno, $errstr, 30);

if (!$sock) {
    die("Connection failed.  $errno: $errstr\n");
}

request($sock, $host, 'GET', '/');

$headers = readResponseHeaders($sock, $resp, $msg);
$body    = readResponseBody($sock, $headers);

echo "Response status: $resp - $msg\n\n";

echo '<pre>' . var_export($headers, true) . '</pre>';
echo "\n\n";
echo $body;

// if the connection is keep-alive, you can make another request here
// as demonstrated below

request($sock, $host, 'GET', '/kernel.css');
$headers = readResponseHeaders($sock, $resp, $msg);
$body    = readResponseBody($sock, $headers);

echo "Response status: $resp - $msg\n\n";

echo '<pre>' . var_export($headers, true) . '</pre>';
echo "\n\n";
echo $body;


exit;

function request($sock, $host, $method = 'GET', $uri = '/', $params = null)
{
    $method = strtoupper($method);
    if ($method != 'GET' && $method != 'POST') $method = 'GET';

    $request = "$method $uri HTTP/1.1\r\n"
              ."Host: $host\r\n"
              ."Connection: keep-alive\r\n"
              ."Accept-encoding: gzip, deflate\r\n"
              ."\r\n";

    fwrite($sock, $request);
}

function readResponseHeaders($sock, &$response_code, &$response_status)
{
    $headers = '';
    $read    = 0;

    while (true) {
        $headers .= fread($sock, 1);
        $read    += 1;

        if ($read >= 4 && $headers[$read - 1] == "\n" && substr($headers, -4) == "\r\n\r\n") {
            break;
        }
    }

    $headers = parseHeaders($headers, $resp, $msg);

    $response_code   = $resp;
    $response_status = $msg;

    return $headers;
}

function readResponseBody($sock, array $headers)
{
    $responseIsChunked = (isset($headers['transfer-encoding']) && stripos($headers['transfer-encoding'], 'chunked') !== false);
    $contentLength     = (isset($headers['content-length'])) ? $headers['content-length'] : -1;
    $isGzip            = (isset($headers['content-encoding']) && $headers['content-encoding'] == 'gzip') ? true : false;
    $close             = (isset($headers['connection']) && stripos($headers['connection'], 'close') !== false) ? true : false;

    $body = '';

    if ($contentLength >= 0) {
        $read = 0;
        do {
            $buf = fread($sock, $contentLength - $read);
            $read += strlen($buf);
            $body .= $buf;
        } while ($read < $contentLength);

    } else if ($responseIsChunked) {
        $body = readChunked($sock);
    } else if ($close) {
        while (!feof($sock)) {
            $body .= fgets($sock, 1024);
        }
    }

    if ($isGzip) {
        $body = gzinflate(substr($body, 10));
    }

    return $body;
}

function readChunked($sock)
{   
    $body = '';

    while (true) {
        $data = '';

        do {
            $data .= fread($sock, 1);
        } while (strpos($data, "\r\n") === false);

        if (strpos($data, ' ') !== false) {
            list($chunksize, $chunkext) = explode(' ', $data, 2);
        } else {
            $chunksize = $data;
            $chunkext  = '';
        }

        $chunksize = (int)base_convert($chunksize, 16, 10);

        if ($chunksize === 0) {
            fread($sock, 2); // read trailing "\r\n"
            return $body;
        } else {
            $data    = '';
            $datalen = 0;
            while ($datalen < $chunksize + 2) {
                $data .= fread($sock, $chunksize - $datalen + 2);
                $datalen = strlen($data);
            }

            $body .= substr($data, 0, -2); // -2 to remove the "\r\n" before the next chunk
        }
    } // while (true)
}

function parseHeaders($headers, &$response_code = null, &$response_message = null)
{
    $lines  = explode("\r\n", $headers);
    $return = array();

    $response = array_shift($lines);

    if (func_num_args() > 1) {
        list($proto, $code, $message) = explode(' ', $response, 3);

        $response_code    = $code;

        if (func_num_args() > 2) {
            $response_message = $message;
        }
    }

    foreach($lines as $header) {
        if (trim($header) == '') continue;
        list($name, $value) = explode(':', $header, 2);

        $return[strtolower(trim($name))] = trim($value);
    }

    return $return;
}
于 2012-08-04T22:46:42.867 回答
0

以下代码对我来说没有任何问题:

<?php
$f = fsockopen("www.google.de",80);
fwrite($f,"GET / HTTP/1.1\r\n Connection: keep-alive\r\n\r\n");

while($s = fread($f,1024)){
    echo "got: $s";
}
echo "finished;";
?>

有趣的是,如果没有keep-alive,这个例子对我来说就会停滞不前。您可以添加一个可以复制和粘贴并显示您的错误的示例吗?

于 2012-08-05T08:59:22.180 回答