socket 作为 server端
<?php
$host = "182.92.xx5.xx";
$port = "8888";
set_time_limit(0); //最好在CLI模式下运行
$socket = socket_create(AF_INET,SOCK_STREAM,0) or die("could not create socket\n");
$result = socket_bind($socket,$host,$port) or die("could not bind to socket\n");
$result = socket_listen($socket,3) or die("could not set up socket listener\n");
//接收连接请求并调用另一个子socket处理客户端
$spawn = socket_accept($socket) or die("could not accept incoming connection\n");
$input = socket_read($spawn,1024) or die("could not read input\n");
$input = trim($input);
//反转客户端输入数据,返回服务端
$output = strrev($input)."\n";
//$output = 'I am server';
socket_write($spawn,$output,strlen($output)) or die("could not write output\n");
socket_close($spawn);
socket_close($socket);
socket 作为client端代码
注意: 在CLI模式下运行
<?php
$sock = fsockopen("182.92.1xx.xx",8888,$errno,$errstr,1);
if(!$sock){
echo "$errstr($errno)<br/>\n";
}else{
socket_set_blocking($sock,false);
fwrite($sock,"hello socket .\r\n");
fwrite($sock,"end\r\n");
while(!feof($sock)){
echo fread($sock,128);
flush();
//ob_flush();
sleep(1);
}
fclose($sock);
}
socket 抓取http数据 (也是作为client端)
注意 GET POST 方式 可以 但是 multipart/form-data 类型可能不行我是没成功还没有报错
可以 结合 wireshark findnler 等抓包软件查看需要传的参数
GET 方式例子
$fp = fsockopen("www-test.funshxxxg.cn",80,$errno,$errstr,5);
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www-test.funsxxxang.cn\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0\r\n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$out .= "Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2\r\n";
$out .= "Accept-Encoding: gzip, deflate\r\n";
$out .= "Cookie: Hm_lvt_71335a21d40d4768497fc64d83607b9b=1509678116; Hm_lvt_e6a72d5590e07f1ad8914ce0a735b069=1509678144; Hm_lvt_71335a21d40d4768497fc64d83607b9b=; Hm_lvt_e6a72d5590e07f1ad8914ce0a735b069=1509678144; fun_session=857338710";
$out .= "Connection: keep-alive\r\n";
$out .= "Upgrade-Insecure-Requests: 1\r\n";
$out .= "Pragma: no-cache\r\n";
$out .= "Cache-Control: no-cache\r\n";
$out .= "\r\n";
fwrite($fp,$out);
while(!feof($fp)){
echo fgets($fp,1280);
}
fclose($fp);