socket方式:
$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); //里面的换行代表 \r\n 注意拷贝的代码后面可能有空格 $http = <<<eof GET / HTTP/1.0 Accept: */* User-Agent: Lowell-Agent Host: www.baidu.com Connection: Close eof; socket_write($socket, $http, strlen($http)); while($str = socket_read($socket, 1024)) { echo $str; } socket_close($socket);
fsockopen方式:
$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); }
原始socket方式:
$fp = stream_socket_client("tcp://www.baidu.com:80", $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $http = <<<eof GET / HTTP/1.0 Accept: */* User-Agent: Lowell-Agent Host: www.baidu.com Connection: Close eof; fwrite($fp, $http); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp); }
时间: 2024-10-12 23:32:15