使用php模拟post提交数据

本文介绍了一种使用PHP来模拟POST请求的方法,通过直接构建HTTP头部信息及请求体内容,实现向指定URL发送POST请求的过程。该方法适用于需要进行简单的HTTP交互测试或数据提交的场景。

使用php模拟post提交数据

PHP代码
  1. <?php   
  2. $target = "http://www.xxxx.com/xxxx.php";   
  3. $host = "www.xxxx.com";   
  4. $msg = "id=1&pw=1";   
  5. $httpheader = "POST ".$target." HTTP/1.1\r\n";   
  6. $httpheader .= "Accept:*/*\r\n";   
  7. $httpheader .= "Accept-Language:zh-cn\r\n";   
  8. $httpheader .= "Referer:".$target."\r\n";   
  9. $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";   
  10. $httpheader .= "User-Agent:Mozilla/4.0(compatible;MSIE 7.0;Windows NT 5.1)\r\n";   
  11. $httpheader .= "Host:".$host."\r\n";   
  12. $httpheader .= "Content-Length:".strlen($msg)."\r\n";   
  13. $httpheader .= "Connection:Keep-Alive\r\n";   
  14. $httpheader .= "\r\n";   
  15. $httpheader .= $msg;   
  16. $fd = fsockopen($host,80);   
  17. fwrite($fd,$httpheader);   
  18. $gets = "";   
  19. while(!feof($fd)){   
  20. $gets .= fread($fd,8192);   
  21. }   
  22. fclose($fd);   
  23. echo "ok";   
  24. echo $gets;   
  25. ?>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值