首页 > 代码库 > socket 发送请求
socket 发送请求
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 = <<<eofGET / HTTP/1.0Accept: */*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方式:
$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 = <<<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 发送请求
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。