自动填写网页表单和返回结果页面 [英] Automatically filling out web forms and returning the resulting page

查看:275
本文介绍了自动填写网页表单和返回结果页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在这里发帖。关于这个问题我非常AP preciate任何和所有的指导。

This is my first time posting here. I greatly appreciate any and all guidance on this subject.

我试图做一个程序,在网页表单自动填写并提交数据时,结果页面返回给程序,以便它可以继续浏览页面,允许它递归提交更多的数据。

I'm trying to make a program that automatically fills in web forms and submits the data, returning the resulting page to the program so it can continue to 'browse' the page, allowing it to recursively submit even more data.

时遇到的主要问题是:


  • 提交按钮在Javascript $ C $的CD,所以我不知道从哪里发出页请求时,表单数据去。

  • 我要填写使用从Excel表中的数据形式,所以我需要能够从页面之外的数据。

  • 我需要能够浏览结果页面继续向更多的数据。

更具体地说,我试图第一次登录到实践伴侣网站,导航到管理病人',点击'添加病人,并以适当的形式填写并提交。
长我填补从Excel表格几千行的形式。结果
对不起,我不能没有提供用户名和密码,可以在此更清楚。

More specifically, I'm trying to first login to the Practice Mate website, navigate to 'Manage Patients', hit 'Add Patients', and fill in the proper forms and submit. I'm filling in the forms from an Excel table thousands of rows long.
Sorry I can't be more clear on this without providing a username and password.

我一直试图做的是使用JavaScript从使用PHP的Excel文档检索信息的页面使页面请求。我似乎仍不能得到任何用这种方法,虽然工作。

What I've been trying to do is use Javascript to make page requests from a page that retrieves information from the Excel document using PHP. I still can't seem to get anything to work with this method though.

我为是在这一个相对的新手道歉。先谢谢了。

I apologize for being a relative novice at this. Thanks in advance.

推荐答案

您可以使用 PHP卷曲浏览&安培;提交表单到网站,但它确实取决于网站是如何设置。最有安全检查到位,以prevent机器人和可能会非常棘手得到的一切工作的权利。

You can use PHP cURL to browse & submit forms to websites, but it does depend on how the website is setup. Most have security checks in place to prevent bots and can be tricky to get everything to work right.

我花了一点点时间以及与此登录脚本走了过来。如果没有有效的用户名和密码,我不能验证它是成功的,但应该做你所需要的。这个简短的例子首次浏览设置任何cookie和凑需要提交表格__VIEWSTATE值的页面。然后,它使用用户名/密码,您提供的提交表单。

I spent a little bit of time and came up with this login script. Without a valid username and password I can't verify that it is successful, but should do what you need. This short example first browses to the page to set any cookies and scrape a __VIEWSTATE value needed to submit the form. It then submits the form using the username/password you provide.

<?php

// Login information
$username = 'test';
$password = 'mypass';
$utcoffset = '-6';
$cookiefile = '/writable/directory/for/cookies.txt';

$client = new Client($cookiefile);

// Retrieve page first to store cookies 
$page = $client -> get("https://pm.officeally.com/pm/login.aspx");
// scrape __VIEWSTATE value
$start = strpos($page, '__VIEWSTATE" value="') + 20;
$end = strpos($page, '"', $start);
$viewstate = substr($page, $start, $end - $start);

// Do our actual login
$form_data = array(
    '__LASTFOCUS' => '', 
    '__EVENTTARGET' => '',
    '__EVENTARGUMENT' => '',
    '__VIEWSTATE' => $viewstate,
    'hdnUtcOffset' => $utcoffset,
    'Login1$UserName' => $username,
    'Login1$Password' => $password,
    'Login1$LoginButton' => 'Log In'
);
$page = $client -> get("https://pm.officeally.com/pm/login.aspx", $form_data);

// cURL wrapper class    
class Login {
    private $_cookiefile;

    public function __construct($cookiefile) {
        if (!is_writable($cookiefile)) {
            throw new Exception('Cannot write cookiefile: ' . $cookiefile);
        }
        $this -> _cookiefile = $cookiefile;
    }

    public function get($url, $referer = 'http://www.google.com', $data = false) {
        // Setup cURL
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this -> _cookiefile);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this -> _cookiefile);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

        // Is there data to post
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }

        return curl_exec($ch);
    }

}

这篇关于自动填写网页表单和返回结果页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆