当前位置:Gxlcms > PHP教程 > php模拟post发送数据

php模拟post发送数据

时间:2021-07-01 10:21:17 帮助过:23人阅读

  1. <?PHP
  2. $flag = 0;
  3. //要post的数据
  4. $argv = array(
  5. 'var1'=>'abc',
  6. 'var2'=>'你好吗');
  7. //构造post字符串
  8. foreach ($argv as $key=>$value) {
  9. if ($flag!=0) {
  10. $params .= "&";
  11. $flag = 1;
  12. }
  13. $params.= $key."="; $params.= urlencode($value);
  14. $flag = 1;
  15. }
  16. $length = strlen($params);
  17. //创建socket连接
  18. $fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
  19. //构造post请求的头
  20. $header = "POST /mobile/try.php HTTP/1.1";
  21. $header .= "Host:127.0.0.1";
  22. $header .= "Referer:/mobile/sendpost.php";
  23. $header .= "Content-Type: application/x-www-form-urlencoded";
  24. $header .= "Content-Length: ".$length."";
  25. $header .= "Connection: Close";
  26. //添加post的字符串
  27. $header .= $params."";
  28. //发送post的数据
  29. fputs($fp,$header);
  30. $inheader = 1;
  31. while (!feof($fp)) {
  32. $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
  33. if ($inheader && ($line == "n" || $line == "")) {
  34. $inheader = 0;
  35. }
  36. if ($inheader == 0) {
  37. echo $line;
  38. }
  39. }
  40. fclose($fp);
  41. ?>

php, post

人气教程排行