Recently, the project has been completed in the background, but the template on the foreground has not been completed, so testing is quite troublesome. So I wrote a simple script to simulate form submission through curl. You can submit data using arrays and strings.
Copy codeThe Code is as follows:
<? Php
/**
* Class SimulantForm simulated form
*/
Class SimulantForm {
/**
* @ Var the url of the page to be submitted
*/
Protected $ _ url;
/**
* @ Var resource curl_init () returns the curl handle
*/
Protected $ _ ch;
/**
* Initialize a form.
* @ Param $ _ url
*/
Public function _ construct ($ _ url ){
$ This-> _ ch = curl_init ();
$ This-> setUrl ($ _ url );
Curl_setopt ($ this-> _ ch, CURLOPT_RETURNTRANSFER, 1 );
}
/**
* Submit in get Mode
* @ 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 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;
}
/**
* Error message returned
* @ Return array [0]: Error code, array [1]: error message
*/
Public function getLastError (){
Return array (curl_errno ($ this-> _ ch), curl_error ($ this-> _ ch ));
}
/**
* SETOPT_COOKIEFILE
* @ Param string $ _ true path of the cookieFile File
*/
Public function setCookieFile ($ _ cookieFile ){
Curl_setopt ($ this-> _ ch, CURLOPT_COOKIEFILE, $ _ cookieFile );
}
/**
* SETOPT_COOKIEJAR
* @ Param string $ _ true path of the cookieFile File
*/
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 method 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 the post method is submitted.
* @ Param array | string $ _ post_data
*/
Protected function _ setPostData ($ _ post_data ){
Curl_setopt ($ this-> _ ch, CURLOPT_POSTFIELDS, $ _ post_data );
}
/**
* Parses the submitted array information into a string for get-based 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 );
}
}