点击提交按钮使用PHP
问题描述:
我有一个网页提交按钮,我想php解析网页,并单击提交按钮,并获得响应(它可以是一个链接或另一个HTML页面。)点击提交按钮使用PHP
有什么办法可以点击使用PHP提交按钮?
我知道有类似java的htmlunit,它允许用户在语法上填充表单域并单击提交按钮。但我想在php中做同样的事情。
感谢
答
卷曲会让你得到表单提交的结果
如
$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流功能
例如
$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);
}
在这两种情况下,$内容应包含表单提交
答
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.
嗨, 的结果如果点击后网页提交按钮被重定向到webpage_1然后,该内容将这个节目?网页内容或网页1的内容? 我也会试一试。 – user244724 2010-01-07 11:05:19