_php tutorial for form classes submitted via Curl simulation post and get mode

Source: Internet
Author: User
Recently done the project, the backstage has been done but the front of the template has not come down, so the test is more troublesome. A simple script was written to simulate the form submission by curl. Data can be submitted in both array and string form.

Copy the Code code as follows:

/**
* Class simulantform Demo Form
*/
Class Simulantform {
/**
* @var The URL of the page to submit
*/
protected $_url;

/**
* @var Resource Curl_init () returns the curl handle
*/
protected $_ch;

/**
* Initialize a form
* @param $_url URL
*/
Public function __construct ($_url) {
$this->_ch = Curl_init ();
$this->seturl ($_url);
curl_setopt ($this->_ch, Curlopt_returntransfer, 1);
}

/**
* Get mode Submit
* @param array|string form data
* @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 mode submission
* @param array|string form data
* @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 error message
* @return Array array[0]: Error number, ARRAY[1]: Error message
*/
Public Function GetLastError () {
Return Array (Curl_errno ($this->_ch), Curl_error ($this->_ch));
}

/**
* Set Setopt_cookiefile
* @param string $_cookiefile file True Path
*/
Public Function Setcookiefile ($_cookiefile) {
curl_setopt ($this->_ch, Curlopt_cookiefile, $_cookiefile);
}

/**
* Set Setopt_cookiejar
* @param string $_cookiefile file True Path
*/
Public Function Setcookiejar ($_cookiefile) {
curl_setopt ($this->_ch, Curlopt_cookiejar, $_cookiefile);
}

/**
* Set URL
* @param $_url
*/
protected function SetUrl ($_url) {
$this->_url = $_url;
curl_setopt ($this->_ch, Curlopt_url, $_url);
}

/**
* Set the data when the Get mode is submitted
* @param $_get_data string or array
* @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;
}
}

/**
* Set the data when post is submitted
* @param array|string $_post_data
*/
protected function _setpostdata ($_post_data) {
curl_setopt ($this->_ch, Curlopt_postfields, $_post_data);
}

/**
* Parse information in the form of a submitted array to a string for a Get method submission
* @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.html www.bkjia.com true http://www.bkjia.com/PHPjc/759967.html techarticle recently done the project, the backstage has been done but the front of the template has not come down, so the test is more troublesome. A simple script was written to simulate the form submission by curl. Can pass ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.