English 中文(简体)
clicking on submit button using 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

最佳回答

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

问题回答

Take a look at Selenium Web application testing system.

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

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.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签