当我发送 HTTP 请求时,我想Connection: Keep-Alive
在需要一个接一个地发送几个请求时使用。如何检查套接字是否包含数据?我的预期代码:
$soc = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$t = socket_connect($soc, gethostbyname('www.example.com'), 80);
if (!$t) {
die(socket_strerror(socket_last_error($soc)));
}
$request = "GET /index.php HTTP/1.1\r\n";
$request .= "Host: www.example.com\r\n";
$request .= "Connection: Keep-Alive\r\n";
$request .= "\r\n";
socket_write($soc, $request);
$buffer = '';
$html = '';
while (socket_data_available($soc)/* THIS FUNCTION DOES NOT EXIST*/) {
socket_recv($soc, $buffer, 2048, MSG_WAITALL);
$html .= $buffer;
}