一個類比的表單類,可以類比post和get方式提交。

來源:互聯網
上載者:User
最近做項目,後台已經做好了但是前台的模版還沒下來,所以測試比較麻煩。於是寫了個簡單的指令碼通過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. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.