Home > Backend Development > PHP Tutorial > Sample code for PHP simulated POST submission

Sample code for PHP simulated POST submission

WBOY
Release: 2016-07-25 08:55:26
Original
1033 people have browsed it
  1. /**
  2. * CURL simulates Post submission in PHP
  3. * by bbs.it-home.org
  4. */
  5. $url = 'http://website/a.php';
  6. $fields = array(
  7. 'UserName'=>urlencode(' a'),
  8. 'PWD'=>urlencode('b') ,
  9. 'AppReturn'=>urlencode('c') ,
  10. 'AppSQL'=>urlencode('d') ,
  11. );
  12. $fields_string = http_build_query($fields);
  13. $ch = curl_init() ;
  14. curl_setopt($ch, CURLOPT_URL,$url) ; //Set the URL to be collected
  15. curl_setopt($ch, CURLOPT_POST,1) ; //Set The form is POST
  16. curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string); //Set the Post parameters
  17. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //Print it with a string.
  18. $re = curl_exec($ch);//(If CURLOPT_RETURNTRANSFER is not set to 1, then the HTML document content will be executed and obtained, such as: 1; otherwise the result will be returned instead of executed)
  19. print_r($re );
  20. $info = curl_getinfo($ch);//Requested information
  21. print_r($info);
  22. $data = array ('UserName' => 'bar');
  23. $data = http_build_query($data );
  24. $opts = array (
  25. 'http' => array (
  26. 'method' => 'POST',
  27. 'header'=> "Content-type: application/x-www-form-urlencodedrn" .
  28. "Content-Length: " . strlen($data) . "rn",
  29. 'content' => $data
  30. ),
  31. );
  32. $context = stream_context_create($opts);
  33. $html = file_get_contents( $url, false, $context);
  34. echo $html;
  35. var_dump($http_response_header);
Copy code

>>> Further reading: php code to implement simulated get and post requests Examples of POST data using socket, curl, file_get_contents methods The length limit of $_GET $_POST parameters in php Post and get application example code in php curl Simple example of php curl post Example code for php curl to implement get, post and cookie How to use php to receive parameters and obtain post original data A simple example of php curl submitting GET, POST and Cookie Usage of $_GET, $_POST, $_REQUEST and $_SERVER in php How to batch process POST values ​​in php Solution to the problem of automatically adding escape characters when passing parameters in PHP form POST php implements post php curl function to simulate post transmission data Example of php curl simulating post request php fsockopen simulates POST submitted code



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