php发送与接受http自动请求,使用socket操作,http自定义herader。;
1,文件 1.php
$A=trim(urlencode('aaaa'));
$B=trim(urlencode('bbbb'));
$C=trim(urlencode('ccccc'));
$params = "Content-A=$A&Content-B=$B&Content-C=$C";
$length = strlen($params);
$fp = fsockopen("localhost",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
$header = "POST /2.php HTTP/1.1\r\n";//一般有post, get这两种
$header .= "Host:localhost\r\n";
$header .= "Referer:1.php\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".$length."\r\n";
$header .= "Connection: Close\r\n\r\n";
$header .= $params."\r\n";
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
//by www.jbxue.com
?>
2,文件 2.php
echo "this is the data posted";
echo "
";
print_r($_REQUEST);
echo "
";foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
/*function GetHeaderInfo(){
foreach ($_SERVER as $name => $value){
$headers[$name] = $value;
}
return $headers;
}
var_dump(GetHeaderInfo());*/
?>