


Form class submitted through curl to simulate post and get methods_PHP tutorial
I have been working on a project recently. The backend has been completed but the template for the frontend has not been downloaded yet, so testing is more troublesome. So I wrote a simple script to simulate form submission through curl. Data can be submitted in two ways: array and string.
/**
* 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);
}
/**
* Parse the submitted information in array form into a string for submission via get method
* @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);
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to implement page jump after PHP form submission [Introduction] In web development, form submission is a common functional requirement. After the user fills out the form and clicks the submit button, the form data usually needs to be sent to the server for processing, and the user is redirected to another page after processing. This article will introduce how to use PHP to implement page jump after form submission. [Step 1: HTML Form] First, we need to write a page containing a form in an HTML page so that users can fill in the data that needs to be submitted.

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

How to use JavaScript to realize the automatic prompt function of the input box content of the form? Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article will introduce how to use JavaScript to achieve this function and provide specific code examples. Create the HTML structure First, we need to create an HTML structure that contains the input box and the auto-suggestion list. You can use the following code: <!DOCTYP

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

If you are an IT administrator or technology expert, you must be aware of the importance of automation. Especially for Windows users, Microsoft PowerShell is one of the best automation tools. Microsoft offers a variety of tools for your automation needs, without the need to install third-party applications. This guide will detail how to leverage PowerShell to automate tasks. What is a PowerShell script? If you have experience using PowerShell, you may have used commands to configure your operating system. A script is a collection of these commands in a .ps1 file. .ps1 files contain scripts executed by PowerShell, such as basic Get-Help

How to handle user rights management in PHP forms With the continuous development of web applications, user rights management is one of the important functions. User rights management can control users' operating rights in applications and ensure the security and legality of data. In PHP forms, user rights management can be implemented through some simple code. This article will introduce how to handle user rights management in PHP forms and give corresponding code examples. 1. Definition and management of user roles First of all, defining and managing user roles is a matter of user rights.
