Home  >  Article  >  Backend Development  >  PHP simulates post to send data

PHP simulates post to send data

WBOY
WBOYOriginal
2016-07-25 08:45:121012browse
  1. $flag = 0;
  2. //Data to be posted
  3. $argv = array(
  4. 'var1'=>'abc',
  5. 'var2'=>'How are you');
  6. //Construct post String
  7. foreach ($argv as $key=>$value) {
  8. if ($flag!=0) {
  9. $params .= "&";
  10. $flag = 1;
  11. }
  12. $params.= $ key."="; $params.= urlencode($value);
  13. $flag = 1;
  14. }
  15. $length = strlen($params);
  16. //Create a socket connection
  17. $fp = fsockopen("127.0.0.1 ",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
  18. //Construct the header of the post request
  19. $header = "POST /mobile/try.php HTTP/1.1";
  20. $header .= "Host:127.0.0.1";
  21. $header .= "Referer:/mobile/sendpost.php";
  22. $header .= "Content-Type: application/x-www- form-urlencoded";
  23. $header .= "Content-Length: ".$length."";
  24. $header .= "Connection: Close";
  25. //Add the post string
  26. $header .= $params. "";
  27. //Send post data
  28. fputs($fp,$header);
  29. $inheader = 1;
  30. while (!feof($fp)) {
  31. $line = fgets($fp,1024); / /Remove the header of the request packet and only display the return data of the page
  32. if ($inheader && ($line == "n" || $line == "")) {
  33. $inheader = 0;
  34. }
  35. if ($inheader == 0) {
  36. echo $line;
  37. }
  38. }
  39. fclose($fp);
  40. ?>
Copy code

php, post


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn