PHP sample code for simulating POST submission

Source: Internet
Author: User
PHP sample code for simulating POST submission

  1. /**
  2. * Simulate Post submission using CURL 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. 'Apprecall' => 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); // you can specify the URL to be collected.
  15. Curl_setopt ($ ch, CURLOPT_POST, 1); // The format is POST.
  16. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string); // you can specify the Post parameter.
  17. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // Print it out with a string.
  18. $ Re = curl_exec ($ ch); // (if CURLOPT_RETURNTRANSFER is not set to 1, the HTML document content will be executed and retrieved, for example, 1; otherwise, the result will be returned, instead of executing)
  19. Print_r ($ re );
  20. $ Info = curl_getinfo ($ ch); // request 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-urlencoded \ r \ n ".
  28. "Content-Length:". strlen ($ data). "\ r \ n ",
  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 );

>>> Additional reading: php code for simulating get and post requests php uses the socket, curl, file_get_contents method to POST data instance php $ _ GET $ _ POST parameter length limit php curl post and get simple example of php curl post using the application instance code php curl to implement get, post and cookie instance code php receiving parameters and methods for obtaining post original data php curl submit GET, POST, Cookie simple instance php $ _ GET, $ _ POST, $ _ REQUEST and $ _ SERVER usage php method for batch processing POST values PHP form POST parameters automatically add escape character problem solution php implementation post php curl simulate post data transmission example of using php curl to simulate a post request php fsockopen to simulate the code submitted by POST

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.