自动填写Web表单并返回结果页面

Bao*_*ozi 3 javascript php automation webforms

这是我第一次在这里发帖.我非常感谢关于这个主题的任何和所有指导.

我正在尝试创建一个程序,自动填写Web表单并提交数据,将结果页面返回到程序,以便它可以继续"浏览"页面,允许它递归提交更多数据.

我遇到的主要问题是:

  • "提交"按钮以Javascript编码,因此在发出页面请求时我不知道表单数据的位置.
  • 我想使用Excel表中的数据填写表单,因此我需要能够从页面外部访问数据.
  • 我需要能够导航结果页面以继续提交更多数据.

更具体地说,我正在尝试首先登录Practice Mate网站,导航到"管理患者",点击"添加患者",并填写正确的表格并提交.我正在填写数千行长的Excel表格中的表格.
抱歉,如果不提供用户名和密码,我就不能更清楚了.

我一直在尝试做的是使用Javascript从一个页面发出页面请求,该页面使用PHP从Excel文档中检索信息.我似乎仍然无法使用这种方法.

我为此成为一名亲戚新手而道歉.提前致谢.

Pas*_*nes 7

您可以使用PHP cURL浏览并向网站提交表单,但这取决于网站的设置方式.大多数都有安全检查来防止机器人,并且可能很难让一切正常工作.

我花了一点时间,想出了这个登录脚本.没有有效的用户名和密码,我无法验证它是否成功,但应该做你需要的.这个简短的示例首先浏览页面以设置任何cookie并刮取提交表单所需的__VIEWSTATE值.然后,它使用您提供的用户名/密码提交表单.

<?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);
    }

}
Run Code Online (Sandbox Code Playgroud)