Home > Backend Development > PHP Tutorial > A simulated form class that can simulate post and get submissions.

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

WBOY
Release: 2016-07-25 08:48:22
Original
852 people have browsed it
In a recent project, the backend has been completed but the frontend template has not yet been downloaded, so testing is more troublesome. So I wrote a simple script to simulate form submission through curl. Data can be submitted in two ways: array and string.
  1. /**
  2. * Class SimulantForm simulation form
  3. */
  4. class SimulantForm {
  5. /**
  6. * @var The page url to be submitted
  7. */
  8. protected $_url;
  9. /**
  10. *@var resource The curl handle returned by curl_init()
  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. *Submit via get method
  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. * Submit via post
  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 real 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 real 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 submitting in get mode
  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 submitting in post mode
  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 the submitted information in array form into a string for submission via get method
  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, and the editing function of oschina is really terrible!
  101. }
  102. }
Copy 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