0

0

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

php中文网

php中文网

发布时间:2016-07-25 08:48:22

|

879人浏览过

|

来源于php中文网

原创

最近做项目,后台已经做好了但是前台的模版还没下来,所以测试比较麻烦。于是写了个简单的脚本通过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. }
复制代码


相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号