使用 php 填写并提交 html 表单,无需用户输入

发布于 2024-11-27 01:38:43 字数 344 浏览 1 评论 0原文

我在名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜夜流光相皎洁 2024-12-04 01:38:43

使用 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.

淡淡绿茶香 2024-12-04 01:38:43

将整个火灾读取到变量中。您可能需要考虑使用 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.

-黛色若梦 2024-12-04 01:38:43

您是否试图让用户在浏览器上提交表单,而无需用户交互?如果是这种情况,您需要求助于 javascript,例如:

<body onLoad="document.getElementById('autoSubmit').submit();">
    <form id="autoSubmit">
    (insert form here) 
    </form>
</body>

这将自动提交表单。一些注意事项:并非每个人都启用了 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:

<body onLoad="document.getElementById('autoSubmit').submit();">
    <form id="autoSubmit">
    (insert form here) 
    </form>
</body>

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 says Click Here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文