我有一个带有提交按钮的网页,我希望php解析网页并单击提交按钮并获得响应(它可以是链接或其他html页面。)
有没有办法用php点击提交按钮?
我知道有一些类似于htmlunit for java,它允许人们以编程方式填写表单字段并单击“提交”按钮。但是我想在php中做同样的事情。
由于
答案 0 :(得分:1)
查看Selenium Web应用程序测试系统。
答案 1 :(得分:0)
SimpleTest PHP库还有一个页面爬虫,可以分析HTML页面并生成相应的POST请求。
答案 2 :(得分:0)
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.
答案 3 :(得分:0)
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都应包含表单提交的结果