PHP simulates post to send data

WBOY
Release: 2016-07-25 08:45:12
Original
1045 people have browsed it
  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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!