duansha3771 2016-03-28 01:03
浏览 17
已采纳

从php脚本填充表单并发送它

I searched the web and found nothing.

On the website, there is a Input box for a E-Mail address. I would like to fill this field with an e-mail address and the send the form. I found this code:

$postdata = http_build_query(
array(
    'email' => 'youremailaddress'
)
); 
$opts = array('http' =>
array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $postdata
)
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

But in my case, the action file is the current page itself and the url is rewritted. So when I put the url like this:

$result = file_get_contents('http://example.com/login.html', false, $context);
var_dump($result);

I get the page but the form is not sent

  • 写回答

2条回答 默认 最新

  • duanli0162 2016-03-28 02:01
    关注

    You may want to check out the Selenium Webdriver & PHPUnit to accomplish this task. You should be able to easily fill out the form and submit it even if the submission URL changes every time. Here's an example of how this would work:

    <?php
    
    class phproTest extends PHPUnit_Extensions_Selenium2TestCase
    {
            protected function setUp()
            {
                    // Which browser to use
                    $this->setBrowser('firefox');
                    // The base URL
                    $this->setBrowserUrl('http://example.com/');
            }
    
            public function testContactFormExists()
            {
                    $this->url( 'http://example.com/login.html' );
    
                    $email = $this->byName( 'sender_email' );
                    $submit = $this->byName( 'submit_button' );
    
                    $this->assertEquals( '', $email->value() );
                    $this->assertEquals( 'Submit', $submit->value() );
            }
    
            public function testSubmitToSelf()
            {
                    // set the url
                    $this->url( 'contact' );
    
                    // create a form object for reuse
                    $form = $this->byId( 'contact_form' );
    
                    // get the form action
                    $action = $form->attribute( 'action' );
    
                    // check the action value
                    $this->assertEquals( 'http://example.com/login.html', $action );
                    // fill in the form field values
                    $this->byName( 'sender_email' )->value( 'youremailhere' );
    
                    // submit the form
                    $form->submit();
            }
    }
    ?>
    

    Looks like a lot but once you break it down, it's not so bad. By using this method, you will be using an actual browser (in this case firefox) to fill out and submit the form. This will load and process javascript which may be needed to create the unique submission URL depending on how your form is created.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?