Socket mode:
$ Socket = socket_create (af_inet, sock_stream, sol_tcp); // socket_set_option ($ socket, sol_socket, so_sndtimeo, array ("Sec" => 20, "USEC" => 0); socket_connect ($ socket, 'www .baidu.com ', 80 ); // The line feed indicates \ r \ n. Note that there may be spaces behind the copied code $ HTTP = <eofget/HTTP/1.0 accept: */* User-Agent: lowell-agenthost: www. baidu. comconnection: closeeof; socket_write ($ socket, $ HTTP, strlen ($ HTTP); While ($ STR = socket_read ($ socket, 1024) {echo $ STR ;} socket_close ($ socket );
Fsockopen mode:
$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);if (!$fp) { echo "$errstr ($errno)<br />\n";} else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.baidu.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $http); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp);}
Original socket method:
$fp = stream_socket_client("tcp://www.baidu.com:80", $errno, $errstr, 30);if (!$fp) { echo "$errstr ($errno)<br />\n";} else { $http = <<<eofGET / HTTP/1.0Accept: */*User-Agent: Lowell-AgentHost: www.baidu.comConnection: Closeeof; fwrite($fp, $http); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp);}
Socket sends request