$url = "www.baidu.com"; Domain name
$path = "/"; Path
$type = "GET"; Request method
Error_reporting (E_all);
echo "
TCP/IP Connection
\ n ";
/* Get the port number */
$service _port = getservbyname (' www ', ' tcp ');
/* Get the IP address. */
$address = gethostbyname ($url);
/* Create a TCP/IP socket. */
$socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
if ($socket = = = False) {
echo "Socket_create () Failed:reason:". Socket_strerror (Socket_last_error ()). "\ n";
} else {
echo "ok.\n";
}
echo "Attempting to connect to ' $address ' on port ' $service _port ' ...";
$result = Socket_connect ($socket, $address, $service _port);
if ($result = = = False) {
echo "Socket_connect () Failed.\nreason: ($result)". Socket_strerror (Socket_last_error ($socket)). "\ n";
} else {
echo "ok.\n";
}
$in = $type. " ". $path." Http/1.1\r\n ";
$in. = "Host:". $url. " \ r \ n ";
$in. = "connection:close\r\n\r\n";
$out = ";
echo "Sending HTTP HEAD request ...";
Socket_write ($socket, $in, strlen ($in));
echo "ok.\n";
echo "Reading response:\n\n";
while ($out = Socket_read ($socket, 2048)) {
Echo $out;
}
echo "Closing socket ...";
Socket_close ($socket);
echo "ok.\n\n";
?>
The above introduces PHP through socket socket to obtain the response content of the Web server, including the content, I hope the PHP tutorial interested in a friend helpful.