A simulated form class that can simulate post and get mode submissions.

Source: Internet
Author: User
Recently done the project, the backstage has been done but the front of the template has not come down, so the test is more troublesome. A simple script was written to simulate the form submission by curl. Data can be submitted in both array and string form.
  1. /**
  2. * Class simulantform Demo Form
  3. */
  4. Class Simulantform {
  5. /**
  6. * @var The URL of the page to submit
  7. */
  8. protected $_url;
  9. /**
  10. * @var Resource Curl_init () returns the curl handle
  11. */
  12. protected $_ch;
  13. /**
  14. * Initialize a form
  15. * @param $_url URL
  16. */
  17. Public function __construct ($_url) {
  18. $this->_ch = Curl_init ();
  19. $this->seturl ($_url);
  20. curl_setopt ($this->_ch, Curlopt_returntransfer, 1);
  21. }
  22. /**
  23. * Get mode Submit
  24. * @param array|string form data
  25. * @return Mixed
  26. */
  27. Public function Get ($_data = ") {
  28. $this->_url. = $this->_setgetdata ($_data);
  29. $this->seturl ($this->_url);
  30. $result = curl_exec ($this->_ch);
  31. Curl_close ($this->_ch);
  32. return $result;
  33. }
  34. /**
  35. * Post mode submission
  36. * @param array|string form data
  37. * @return Mixed
  38. */
  39. Public Function post ($_data) {
  40. curl_setopt ($this->_ch, curlopt_post, 1);
  41. $this->_setpostdata ($_data);
  42. $result = curl_exec ($this->_ch);
  43. Curl_close ($this->_ch);
  44. return $result;
  45. }
  46. /**
  47. * Return error message
  48. * @return Array array[0]: Error number, ARRAY[1]: Error message
  49. */
  50. Public Function GetLastError () {
  51. Return Array (Curl_errno ($this->_ch), Curl_error ($this->_ch));
  52. }
  53. /**
  54. * Set Setopt_cookiefile
  55. * @param string $_cookiefile file True Path
  56. */
  57. Public Function Setcookiefile ($_cookiefile) {
  58. curl_setopt ($this->_ch, Curlopt_cookiefile, $_cookiefile);
  59. }
  60. /**
  61. * Set Setopt_cookiejar
  62. * @param string $_cookiefile file True Path
  63. */
  64. Public Function Setcookiejar ($_cookiefile) {
  65. curl_setopt ($this->_ch, Curlopt_cookiejar, $_cookiefile);
  66. }
  67. /**
  68. * Set URL
  69. * @param $_url
  70. */
  71. protected function SetUrl ($_url) {
  72. $this->_url = $_url;
  73. curl_setopt ($this->_ch, Curlopt_url, $_url);
  74. }
  75. /**
  76. * Set the data when the Get mode is submitted
  77. * @param $_get_data string or array
  78. * @return Mixed
  79. */
  80. protected function _setgetdata ($_get_data) {
  81. if (Is_array ($_get_data)) {
  82. return $this->_getdatatostring ($_get_data);
  83. } elseif (Is_string ($_get_data)) {
  84. return $_get_data;
  85. }
  86. }
  87. /**
  88. * Set the data when post is submitted
  89. * @param array|string $_post_data
  90. */
  91. protected function _setpostdata ($_post_data) {
  92. curl_setopt ($this->_ch, Curlopt_postfields, $_post_data);
  93. }
  94. /**
  95. * Parse information in the form of a submitted array to a string for a Get method submission
  96. * @param array $_get_data
  97. * @return String
  98. */
  99. protected function _getdatatostring (array $_get_data) {
  100. Return '? '. Http_build_query ($_get_data); Refer to the first floor, replaced by the Http_build_query function, the other Oschina editing function is too disabled!
  101. }
  102. }
Copy Code
  • 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.