Use curl to simulate form classes submitted by post and get methods. 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 use the latest project, and the background has been completed, but the foreground template has not yet come down, so testing is troublesome. So I wrote a simple script to simulate form submission through curl. You can submit data using arrays and strings.
The code is as follows:
/**
* 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 );
}
}
Bytes. So I wrote a simple script to simulate form submission through curl. You can use...