php互相发送请求,php 发送和接受自动http请求的实例分享

这篇博客详细介绍了如何使用PHP的socket函数发送一个带有自定义Header的POST请求到本地服务器。示例代码展示了如何构造请求头、编码参数、连接服务器并读取响应。在2.php中,数据被接收并打印出来,同时展示接收到的所有HTTP头部信息。
摘要由CSDN通过智能技术生成

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());*/

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值