Home > Backend Development > PHP Tutorial > PHP uses curl to implement simulated form submission function

PHP uses curl to implement simulated form submission function

墨辰丷
Release: 2023-03-27 13:24:01
Original
1985 people have browsed it

This article mainly introduces in detail how PHP uses curl to implement a simple simulated form submission function. It has a certain reference value. Interested friends can refer to it

The specific content is as follows

//初始化curl
$ch = curl_init();
$url = 'xxx';
$option = [
 CURLOPT_URL => $url,
 CURLOPT_HEADER => 1,
 CURLOPT_RETURNTRANSFER => 1,
];
curl_setopt_array($ch, $option);
$output = curl_exec($ch);
preg_match_all('/Set-Cookie: (.*);/i', $output, $str); //正则匹配
//如果有token的话就获取token
preg_match('/token = \"(.*)\"/i', $output, $token);
if (empty($str)) {
 return false;
}
$cook = $str[1];
$cookie = implode(';', $cook);

$up_url = 'xxx';
$post_data = [
 'name' => 'test_',
 'tel' => '18819271234',
 'email' => 'qqhahadfdfads@163.com',
 'message' => 'this is my message;',
 '_token' => $token[1],
];
$options = [
 CURLOPT_URL => $up_url,
 CURLOPT_RETURNTRANSFER => 1,
 CURLOPT_HEADER => 1,
 CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36",
 CURLOPT_COOKIESESSION => 1,
 CURLOPT_POST => 1,
 CURLOPT_POSTFIELDS => $post_data,
 CURLOPT_COOKIE => $cookie
];
curl_setopt_array($ch, $options);
$res = curl_exec($ch);
// $info = curl_getinfo($ch);
if ($res === FALSE) {
 p('curl Error: ' . curl_error($ch));
}
curl_close($ch);
p($res);
Copy after login

Taken from a website developed based on laravel. There is a csrf_token in the form in laravel. So you have to get the token and bring the cookie

Note here that you cannot initialize two curls at the same time, otherwise the token or cookie will change. If you need to initialize two curls, you can use the CURLOPT_COOKIEJAR setting. [See the manual for details]

Then run Next: binggo

When using curl, the most important thing is the application of cookies. After looking through the manual, there are four cookie settings. ..CURLOPT_COOKIESESSION, CURLOPT_COOKIE, CURLOPT_COOKIEFILE, CURLOPT_COOKIEJAR

Related recommendations:

php uses curlmethod to implement ftp file download function

php sends XML data through curl and gets XML data

php curlgets the return Detailed explanation of value steps

The above is the detailed content of PHP uses curl to implement simulated form submission function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template