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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化