使用 php 单击提交按钮

发布于 2024-08-17 03:48:17 字数 187 浏览 4 评论 0原文

我有一个带有提交按钮的网页,我希望 php 解析该网页并单击提交按钮并获取响应(它可以是链接或另一个 html 页面。)

有什么方法可以使用 php 单击提交按钮吗?

我知道有类似 htmlunit for java 的东西,允许人们以编程方式填写表单字段并单击提交按钮。但我想在 php 中做同样的事情。

谢谢

I have a webpage with a submit button and I would like php to parse the webpage and click the submit button and get the response (it can be a link or another html page.)

Is there any way to click a submit button using php?

I know there is something like htmlunit for java that allows one to pro-grammatically fill the form fields and click submit button. But I would like to do the same in php.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

奶气 2024-08-24 03:48:17

看一下 Selenium Web 应用程序测试系统。

Take a look at Selenium Web application testing system.

耳钉梦 2024-08-24 03:48:17

CURL 可以让您获取表单提交的结果

,例如

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $urlOfFormSubmission);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(

        "field1"=>"data1",
        "field2"=>"data2"

    ));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$contents = curl_exec($ch);

您也可以使用 PHP Stream 函数执行相同的操作

,例如

$params = array('http' => array(
          'method' => "post",
          'content' => array("field1"=>"data1", "field2"=>"data2")
       ));

$ctx = stream_context_create($params);

$fp = @fopen($urlOfFormSubmission, 'rb', false, $ctx);

if (!$fp)
{
    throw new Error("Problem with ".$urlOfFormSubmission);
}

$contents = @stream_get_contents($fp);

if ($contents === false)
{
    throw new Error("Problem reading data from ".$urlOfFormSubmission);
}

无论哪种情况,$contents 都应该包含表单提交的结果

CURL will let you get the results of a form submission

eg

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $urlOfFormSubmission);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(

        "field1"=>"data1",
        "field2"=>"data2"

    ));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$contents = curl_exec($ch);

You can also do the same thing with the PHP Stream functions

eg

$params = array('http' => array(
          'method' => "post",
          'content' => array("field1"=>"data1", "field2"=>"data2")
       ));

$ctx = stream_context_create($params);

$fp = @fopen($urlOfFormSubmission, 'rb', false, $ctx);

if (!$fp)
{
    throw new Error("Problem with ".$urlOfFormSubmission);
}

$contents = @stream_get_contents($fp);

if ($contents === false)
{
    throw new Error("Problem reading data from ".$urlOfFormSubmission);
}

In either case, $contents should contain the results of the form submission

温柔女人霸气范 2024-08-24 03:48:17

SimpleTest PHP 库还有一个页面爬虫,可以分析 HTML 页面并生成适当的 POST要求。

The SimpleTest PHP library also has a page crawler that can analyze a HTML page and generate the appropriate POST request.

空城旧梦 2024-08-24 03:48:17

phpWebHacks 看起来很有希望完成这项任务。

功能,引自网站:

* Support HTTP/1.1
* Fetch web pages.
* Submit forms and upload files.
* Support https.
* Support HTTP cookies.
* Support HTTP redirects and Meta-refresh redirects.
* Support HTTP Authentication.
* Support proxy server.
* Support gzip encoding.
* Logging of HTTP streams for full debugging.
* Parsing HTML forms.
* Custom User-Agent.

phpWebHacks looks promising for the task.

Features, as quoted from the website:

* Support HTTP/1.1
* Fetch web pages.
* Submit forms and upload files.
* Support https.
* Support HTTP cookies.
* Support HTTP redirects and Meta-refresh redirects.
* Support HTTP Authentication.
* Support proxy server.
* Support gzip encoding.
* Logging of HTTP streams for full debugging.
* Parsing HTML forms.
* Custom User-Agent.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文