我正在尝试创建一个简单的cURL连接来填写POST表单,但由于某种原因它不是......做它应该做的事情?
这是我的代码;
HTML表单
<html>
<body>
<form method="post" action="test.php">
Name <input type="text" name="name"><br>
Username <input type="text" name="username"><br>
Email <input type="text" name="email"><br>
Pass: <input type="text" name="password"><br>
Pass2: <input type="text" name="password2"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP表格
<?php
$con = mysqli_connect('localhost', 'root', '', 'hawbaw') or mysqli_error($con);
$name = $_POST['name'];
$username = $_POST['username'];
$email = $_POST['email'];
$pass = $_POST['password'];
$pass2 = $_POST['password2'];
mysqli_query($con, "INSERT INTO haw (`id`, `name`, `username`, `password`, `email`) VALUES (NULL, '$name', '$username', '$pass', '$email')");
?>
cURL连接
<?php
$data = array();
$data['name'] = '123';
$data['username'] = '123';
$data['email'] = '123';
$data['password'] = '123';
$data['password2'] = '123';
$data['submit'] = 'Submit';
$post_str = '';
foreach ($data as $key=>$value) {
$post_str.= $key . '=' . urlencode($value) . '&';
}
$post_str = substr($post_str, 0, -1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test.html');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
?>
cURL数据
curl 'http://localhost/test.php' -H 'Origin: http://localhost' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'Referer: http://localhost/test.html' -H 'Connection: keep-alive' --data 'name=21312&username=21321&email=21312&password=21321&password2=12321&submit=Submit' --compressed