使用 php 填写并提交 html 表单,无需用户输入
我在名为“foobar.html”的文件中有以下表单:
<!-- other stuff -->
<form method="post" action="foo.php?cat=1">
<input type="text" name="bar" />
<input type="submit" value="foobar" name="foobar" />
</form>
<!-- other stuff -->
并且我使用 fopen 在 php 脚本中打开此文件,如何在没有用户任何输入的情况下填写并提交此表单?谢谢
I have the following form in a file called "foobar.html":
<!-- other stuff -->
<form method="post" action="foo.php?cat=1">
<input type="text" name="bar" />
<input type="submit" value="foobar" name="foobar" />
</form>
<!-- other stuff -->
And I open this file in a php script with fopen, how do I fill out and submit this form without any input from the user? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 HTML 解析器解析出
action
属性,并使用curl 对适当的目标 URL 执行 POST。Parse out the
action
attribute with a HTML parser, and use curl to perform a POST to the appropriate target URL.将整个火灾读取到变量中。您可能需要考虑使用 file_get_contents ,而不是使用 fopen ,比较干净一点。
然后您需要将该字符串解析为 HTML。您可以使用 PHP 的 DOMDocument 来实现这一点。通过遍历 DOM 树到表单标签并读出这些属性来获取表单的操作和方法。接下来获取表单标签内任何输入的名称。使用这些名称来生成带有键=值对的查询字符串。如果表单的方法是 GET,则将该查询字符串附加到表单操作,否则将其保存在另一个变量中。
最后,使用 CURL 来“提交”表单。即,使用表单操作作为 CURL 请求的 URL。如果表单方法是 GET,您应该已经将数据附加到 URL,如果方法是 POST,您需要将 CURL 请求的数据设置为从表单名称生成的数据查询字符串。
如果您的问题延伸到如何知道要在哪些表单字段中填写哪些数据,那么这几乎是不可能解决的。当然,您可以查找一些输入名称并猜测所需的数据,但通用解决方案是一个不可能解决的问题。
Read the entire fire into a variable. Rather than using fopen you might want to consider file_get_contents for that, it's a bit cleaner.
You'll then want to parse that string as HTML. You could use PHP's DOMDocument for that. Get the action and method of the form by traversing the DOM tree to the form tag and reading out those attributes. Next get the names of any inputs within the form tags. Use those names to generate a query string with your key=value pairs. If the method of the form is GET, then append that query string to the form action, otherwise save it in another variable.
Finally, use CURL to "submit" the form. That is, use the form action as the URL for a CURL request. If the form method was GET, you should have already appended the data to the URL, if the method was POST, you'll want to set the data for the CURL request to the data query string you generated from the form names.
If your question extend to how to know what data to fill into what form fields, that is pretty much impossible to solve. Certainly there are some input names you could look for and guess the required data but a universal solution is an impossible problem to solve.
您是否试图让用户在浏览器上提交表单,而无需用户交互?如果是这种情况,您需要求助于 javascript,例如:
这将自动提交表单。一些注意事项:并非每个人都启用了 JavaScript,因此您可能需要将输入更改为
type="hidden"
,并添加一个漂亮的大提交按钮,上面写着Click Here
。Are you trying to have the user submit the form on their browser, without user interaction? If that's the case, you'll need to resort to javascript, something like:
This will automatically submit the form. Some notes: not everyone has JavaScript enabled, so you might want to change the inputs to
type="hidden"
, as well as add a nice big submit button that saysClick Here
.