Heim  >  Artikel  >  Backend-Entwicklung  >  一个模拟的表单类,可以模拟post和get方式提交。

一个模拟的表单类,可以模拟post和get方式提交。

WBOY
WBOYOriginal
2016-07-25 08:48:22779Durchsuche
最近做项目,后台已经做好了但是前台的模版还没下来,所以测试比较麻烦。于是写了个简单的脚本通过curl的方式模拟表单提交。可以通过数组和字符串两种方式提交数据。
  1. /**
  2. * Class SimulantForm 模拟表单
  3. */
  4. class SimulantForm {
  5. /**
  6. * @var 要提交的页面url
  7. */
  8. protected $_url;
  9. /**
  10. * @var resource curl_init()返回的curl句柄
  11. */
  12. protected $_ch;
  13. /**
  14. * 初始化一个表单
  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方式提交
  24. * @param array|string 表单数据
  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方式提交
  36. * @param array|string 表单数据
  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. * 返回错误信息
  48. * @return array array[0]:错误号 , array[1]:错误信息
  49. */
  50. public function getLastError() {
  51. return array(curl_errno($this->_ch), curl_error($this->_ch));
  52. }
  53. /**
  54. * 设置SETOPT_COOKIEFILE
  55. * @param string $_cookieFile 文件真实路径
  56. */
  57. public function setCookieFile($_cookieFile) {
  58. curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $_cookieFile);
  59. }
  60. /**
  61. * 设置SETOPT_COOKIEJAR
  62. * @param string $_cookieFile 文件真实路径
  63. */
  64. public function setCookieJar($_cookieFile) {
  65. curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $_cookieFile);
  66. }
  67. /**
  68. * 设置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. * 设置get方式提交时的数据
  77. * @param $_get_data 字符串或数组
  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. * 设置post方式提交时的数据
  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. * 将提交的数组形式的信息解析为字符串用于get方式提交
  96. * @param array $_get_data
  97. * @return string
  98. */
  99. protected function _getDataToString(array $_get_data) {
  100. return '?' . http_build_query($_get_data); //参考一楼,换成了http_build_query函数,另外oschina的编辑功能实在是太残了!
  101. }
  102. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn