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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题