使用curl和php自动填写表单
我正在尝试编写一个自动填写表单然后自动按下提交按钮的脚本。
我读过您可以使用curl来发布HTTP请求,但是当表单使用JavaScript处理post请求时,您会做什么,就像下面的代码一样?
<form name="myform" action="javascript:void(0)" method="POST" onsubmit="return fancy_function(this)">
I am trying to write a script that fills out forms automatically and then automatically presses the submit button.
I have read that you can use curl to post HTTP requests, but what do you do when the form handles the post request with JavaScript, like the code below?
<form name="myform" action="javascript:void(0)" method="POST" onsubmit="return fancy_function(this)">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您对上述评论的回答,您想要做的不是用curl“填写表单”,而是将完成的表单发布到表单通常发送到的同一页面。
这也称为跨站点发布,许多开发人员专门测试并不允许它提高安全性。如果表单有验证码,这也会困难得多。
假设这样做仍然有意义(我之前在新闻通讯注册表单中使用过这种技术),这就是您想要做的:
要获取表单的操作 URL,您需要查看该网站的 Javascript 代码并找到 funcy_function() 的定义位置。然后在函数主体中,您可能会看到目标 url。您还需要记下 html 中表单变量的具体名称。您需要使用完全相同的名称设置变量。然后你的curl 设置将如下所示:
这会将post 变量发送到指定的url,模仿表单中通常发生的情况。如果您想解析或使用页面返回的 html,它将位于 $response 中。
Based off your answer to the comments above, what you want to do is not "fill out the form" with curl, but rather post the completed form to the same page the form would normally send to.
This is also called cross-site posting, and many developers specifically test for and don't allow it to improve security. This will also be much much harder if the form has a captcha.
Assuming that it still makes sense to do (I've used this technique before for a newsletter signup form), here's what you want to do:
To get the form's action url, you'll need to look through the Javascript code for the site and find where funcy_function() is defined. Then in the body of the function, you'll likely see the destination url. You'll also need to note the specific names of the form variables in the html. You'll need to setup your variables with the exact same names. Then your curl setup will look like this:
This will send the post variables to the specified url, imitating what would normally happen from the form. The html that the page returns will be in $response if you want to parse or use it.
如果您想使用 cURL:
您可以使用 Firefox Web-Tools 捕获每个 HTTP 请求(GET 或 POST 表单):
Web-Tools ->网络->发送您的表格 ->表单 URL 的上下文菜单(右键单击表单脚本)->复制=>复制为 cURL 地址
更多信息:https://everything.curl.dev/usingcurl/copyas
If you want to use cURL:
You can capture every HTTP request (GET or POST forms) with Firefox Web-Tools:
Web-Tools -> Network -> send your form -> context menu of the form URL (right click on form script) -> Copy => copy as cURL address
More infos: https://everything.curl.dev/usingcurl/copyas