通過curl類比post和get方式提交的表單類_PHP教程

來源:互聯網
上載者:User
最近做項目,後台已經做好了但是前台的模版還沒下來,所以測試比較麻煩。於是寫了個簡單的指令碼通過curl的方式類比表單提交。可以通過數組和字串兩種方式提交資料。

複製代碼 代碼如下:

/**
* Class SimulantForm 類比表單
*/
class SimulantForm {
/**
* @var 要提交的頁面url
*/
protected $_url;

/**
* @var resource curl_init()返回的curl控制代碼
*/
protected $_ch;

/**
* 初始化一個表單
* @param $_url url
*/
public function __construct($_url) {
$this->_ch = curl_init();
$this->setUrl($_url);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
}

/**
* get方式提交
* @param array|string 表單資料
* @return mixed
*/
public function get($_data = '') {
$this->_url .= $this->_setGetData($_data);
$this->setUrl($this->_url);
$result = curl_exec($this->_ch);
curl_close($this->_ch);
return $result;
}

/**
* post方式提交
* @param array|string 表單資料
* @return mixed
*/
public function post($_data) {
curl_setopt($this->ch, CURLOPT_POST, 1);
$this->_setPostData($_data);
$result = curl_exec($this->_ch);
curl_close($this->_ch);
return $result;
}

/**
* 返回錯誤資訊
* @return array array[0]:錯誤號碼 , array[1]:錯誤資訊
*/
public function getLastError() {
return array(curl_errno($this->_ch), curl_error($this->_ch));
}

/**
* 設定SETOPT_COOKIEFILE
* @param string $_cookieFile 檔案真實路徑
*/
public function setCookieFile($_cookieFile) {
curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $_cookieFile);
}

/**
* 設定SETOPT_COOKIEJAR
* @param string $_cookieFile 檔案真實路徑
*/
public function setCookieJar($_cookieFile) {
curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $_cookieFile);
}

/**
* 設定url
* @param $_url
*/
protected function setUrl($_url) {
$this->_url = $_url;
curl_setopt($this->_ch, CURLOPT_URL, $_url);
}

/**
* 設定get方式提交時的資料
* @param $_get_data 字串或數組
* @return mixed
*/
protected function _setGetData($_get_data) {
if(is_array($_get_data)) {
return $this->_getDataToString($_get_data);
} elseif(is_string($_get_data)) {
return $_get_data;
}
}

/**
* 設定post方式提交時的資料
* @param array|string $_post_data
*/
protected function _setPostData ($_post_data) {
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $_post_data);
}

/**
* 將提交的數組形式的資訊解析為字串用於get方式提交
* @param array $_get_data
* @return string
*/
protected function _getDataToString(array $_get_data) {
$result_string = '?';
array_walk($_get_data, function ($value, $key) use (&$result_string) {
if(is_array($value)) {
foreach($value as $sec_value) {
$result_string .= $key . '[]=' . $sec_value . '&';
}
} else {
$result_string .= $key . '=' . $value . '&';
}
});
return substr($result_string, 0, strlen($result_string) - 1);
}
}

http://www.bkjia.com/PHPjc/759967.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/759967.htmlTechArticle最近做項目,後台已經做好了但是前台的模版還沒下來,所以測試比較麻煩。於是寫了個簡單的指令碼通過curl的方式類比表單提交。可以通過...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.