如何使用PHP /卷曲发布ASP.NET登录表单? [英] How to post ASP.NET login form using PHP/cURL?

查看:125
本文介绍了如何使用PHP /卷曲发布ASP.NET登录表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个工具,将使用PHP发布ASP.NET登录表单,这样我可以收集他们在登录后显示在用户的摘要页面的详细信息。

由于该网站使用ASP.NET和形式都有__VIEWSTATE和__EVENTVALIDATION隐藏字段,按我的理解,我必须首先获取这些值,然后在POST提交他们的登录表单这个工作。

我是新来的PHP。我所创建的脚本应该做到以下几点:

1)获取登录表单,并抢__VIEWSTATE和__EVENTVALIDATION

2),POST与适当的后数据的登录表单。

3)现在,我验证的GET,应该是访问的页面summary.htm

什么是实际发生的事情我不清楚。发布到登录表单后,我收到一个cookie,但如果该cookie指示我验证不能告诉。当我试图让summary.htm页,我重定向到登录页面,如果我不进行身份验证。

我是新来的PHP,我希望有人在那里谁farmiliar是它也许能够看到一些明显的,我很想念。

下面是code:

 < PHPrequire_once(包括/ simple_html_dom.php);的ini_set('display_errors设置','开');
使用error_reporting(E_ALL);//创建连接卷曲
$ URL ='https://www.mysite.com/account/login.htm';
$ cookieFile ='cookie.txt';
$ CH = curl_init();//我们必须要求登录页面,并获得ViewState中和EventValidation隐藏值
//并通过沿POST请求的。curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setOpt($ CH,CURLOPT_REFERER,'https://www.mysite.com/account/login.htm');
curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列('产地:https://www.mysite.com','主持人:www.mysite.com'));
$ curl_scraped_pa​​ge = curl_exec($ CH);//取得的ViewState和EventValidation数据
$ HTML = str_get_html($ curl_scraped_pa​​ge);
$视图状态= $ HTML的>找到(#__ VIEWSTATE,0);
$ eventValidation = $ HTML的>找到(#__ EVENTVALIDATION,0);
$previousPage = $ HTML的>找到(#__ preVIOUSPAGE,0);
//创建数据阵列发布
//这个比赛正是我看到在看的时候提琴手被张贴
$ post_data ['__ EVENTTARGET'] ='';
$ post_data ['__ EVENTARGUMENT'] ='';
$ post_data ['__ VIEWSTATE'] = $ viewState->价值;
$ post_data ['__ EVENTVALIDATION'] = $ eventValidation->价值;
$ post_data ['__ preVIOUSPAGE'] = $previousPage->价值;
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$txtUsername'] ='BSMITH';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$txtPassword'] ='Weez442';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$chkLoginPersist'] ='上';
$ post_data ['ctl00 $ ctl00 $ cphMasterBody $ cphPageTemplateContent $ MyAccountLogin967 $ btnLogin'] ='登录>';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$LoginModal$LoginFields$txtModalUsername'] ='';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$LoginModal$LoginFields$txtModalPassword'] ='';
$ post_data ['ctl00 $ ctl00 $ cphMasterBody $ cphPageTemplateTopHeader $ IncludeHeader $ SearchForm $ inputText的'] ='';张贴//遍历数组和prepare数据(键1 =值)
的foreach($ post_data为$关键=> $值){
    $ post_items [] = rawurlen code($键)。 =。 rawurlen code($值);
}//创建最终的字符串使用破灭发布()
$ post_string =破灭('和;',$ post_items);//为岗位设置选项
curl_setOpt($ CH,CURLOPT_POST,TRUE);
curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列('产地:https://www.mysite.com','主持人:www.mysite.com','内容类型:应用程序/ x-WWW的形式urlen $ C $光盘'));
curl_setopt($ CH,CURLOPT_POSTFIELDS,$ post_string);
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_COOKIEJAR,$ cookieFile);
curl_setOpt($ CH,CURLOPT_REFERER,'https://www.mysite.com/account/login.htm');//执行我们的POST请求
$ curl_scraped_pa​​ge = curl_exec($ CH);回声$ curl_scraped_pa​​ge;//现在让我们的帐页汇总
$ urlAcctSummary =htt​​ps://www.mysite.com/my-account/summary.htm;
//设置选项
curl_setOpt($ CH,CURLOPT_HTTPGET,TRUE);
curl_setOpt($ CH,CURLOPT_POST,FALSE);
curl_setopt($ CH,CURLOPT_URL,$ urlAcctSummary);
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ CH,CURLOPT_COOKIEFILE,$ cookieFile);$ curl_scraped_pa​​ge = curl_exec($ CH);回声$ curl_scraped_pa​​ge;curl_close($ CH);?>


解决方案

我想通了。我调整在几个方面code,但我相信我的问题的根源是,ASP.NET想从第一个GET请求设置会话cookie,我只对指定的POST请求和CURLOPT_COOKIEFILE的CURLOPT_COOKIEJAR最终的GET请求。

在我把CURLOPT_COOKIEJAR和CURLOPT_COOKIEFILE在其担任设计。最先GET请求

下面是我的code看起来像走动的后:

 < PHPrequire_once(包括/ simple_html_dom.php);的ini_set('display_errors设置','开');
使用error_reporting(E_ALL);//创建连接卷曲
$ URL ='https://www.mysite.com/account/login.htm';
$ cookieFile ='cookie.txt';
$ CH = curl_init();//我们必须要求登录页面,并获得ViewState中和EventValidation隐藏值
//并通过沿POST请求的。curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setOpt($ CH,CURLOPT_REFERER,'https://www.mysite.com/account/login.htm');
curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列('产地:https://www.mysite.com','主持人:www.mysite.com'));
curl_setopt($ CH,CURLOPT_COOKIEJAR,$ cookieFile);
curl_setopt($ CH,CURLOPT_COOKIEFILE,$ cookieFile);
$ curl_scraped_pa​​ge = curl_exec($ CH);//取得的ViewState和EventValidation数据
$ HTML = str_get_html($ curl_scraped_pa​​ge);
$视图状态= $ HTML的>找到(#__ VIEWSTATE,0);
$ eventValidation = $ HTML的>找到(#__ EVENTVALIDATION,0);
$previousPage = $ HTML的>找到(#__ preVIOUSPAGE,0);
//创建数据阵列发布
//这个比赛正是我看到在看的时候提琴手被张贴
$ post_data ['__ EVENTTARGET'] ='';
$ post_data ['__ EVENTARGUMENT'] ='';
$ post_data ['__ VIEWSTATE'] = $ viewState->价值;
$ post_data ['__ EVENTVALIDATION'] = $ eventValidation->价值;
$ post_data ['__ preVIOUSPAGE'] = $previousPage->价值;
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$txtUsername'] ='BSMITH';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$txtPassword'] ='Weez442';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$chkLoginPersist'] ='上';
$ post_data ['ctl00 $ ctl00 $ cphMasterBody $ cphPageTemplateContent $ MyAccountLogin967 $ btnLogin'] ='登录>';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$LoginModal$LoginFields$txtModalUsername'] ='';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$LoginModal$LoginFields$txtModalPassword'] ='';
$ post_data ['ctl00 $ ctl00 $ cphMasterBody $ cphPageTemplateTopHeader $ IncludeHeader $ SearchForm $ inputText的'] ='';张贴//遍历数组和prepare数据(键1 =值)
的foreach($ post_data为$关键=> $值){
    $ post_items [] = rawurlen code($键)。 =。 rawurlen code($值);
}//创建最终的字符串使用破灭发布()
$ post_string =破灭('和;',$ post_items);//为岗位设置选项
curl_setOpt($ CH,CURLOPT_POST,TRUE);
curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列('产地:https://www.mysite.com','主持人:www.mysite.com','内容类型:应用程序/ x-WWW的形式urlen $ C $光盘'));
curl_setopt($ CH,CURLOPT_POSTFIELDS,$ post_string);
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setOpt($ CH,CURLOPT_REFERER,'https://www.mysite.com/account/login.htm');//执行我们的POST请求
$ curl_scraped_pa​​ge = curl_exec($ CH);回声$ curl_scraped_pa​​ge;//现在让我们的帐页汇总
$ urlAcctSummary =htt​​ps://www.mysite.com/my-account/summary.htm;
//设置选项
curl_setOpt($ CH,CURLOPT_HTTPGET,TRUE);
curl_setOpt($ CH,CURLOPT_POST,FALSE);
curl_setopt($ CH,CURLOPT_URL,$ urlAcctSummary);
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);$ curl_scraped_pa​​ge = curl_exec($ CH);回声$ curl_scraped_pa​​ge;curl_close($ CH);?>

I need to create a tool that will post a ASP.NET login form using PHP so that I can gather details from the user's summary page that is displayed after they are logged in.

Because the site uses ASP.NET and the form has __VIEWSTATE and __EVENTVALIDATION hidden fields, as I understand it, I must get those values first, then submit them in the POST to the login form for this to work.

I am new to PHP. The script that I have created should do the following:

1) GET the login form and grab __VIEWSTATE and __EVENTVALIDATION

2) POST to the login form with appropriate post data.

3) GET the summary.htm page that should be accessible now that I am authenticated.

What is actually happening is unclear to me. After POSTing to the login form, I receive a cookie, but can't tell if that cookie indicates I am authenticated. When I try to GET the summary.htm page I am redirected back to the login page as if I am not authenticated.

I am new to PHP and I am hoping that someone out there who is farmiliar with it might be able to see something obvious that I am missing.

Here is the code:

<?php

require_once  ("Includes/simple_html_dom.php");

ini_set('display_errors', 'On');
error_reporting(E_ALL);

// Create curl connection
$url = 'https://www.mysite.com/account/login.htm';
$cookieFile = 'cookie.txt';
$ch = curl_init();

// We must request the login page and get the ViewState and EventValidation hidden values
// and pass those along in the post request.

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setOpt($ch, CURLOPT_REFERER, 'https://www.mysite.com/account/login.htm');
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Origin: https://www.mysite.com', 'Host: www.mysite.com'));


$curl_scraped_page = curl_exec($ch);

// Grab ViewState and EventValidation data
$html = str_get_html($curl_scraped_page);
$viewState = $html->find("#__VIEWSTATE", 0);
$eventValidation = $html->find("#__EVENTVALIDATION", 0);
$previousPage = $html->find("#__PREVIOUSPAGE", 0);


//create array of data to be posted
// This matches exactly what I am seeing being posted when looking at Fiddler
$post_data['__EVENTTARGET'] = '';
$post_data['__EVENTARGUMENT'] = '';
$post_data['__VIEWSTATE'] = $viewState->value;
$post_data['__EVENTVALIDATION'] = $eventValidation->value;
$post_data['__PREVIOUSPAGE'] = $previousPage->value;
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$txtUsername'] = 'bsmith';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$txtPassword'] = 'Weez442';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$chkLoginPersist'] = 'on';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$btnLogin'] = 'Login >';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$LoginModal$LoginFields$txtModalUsername'] = '';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$LoginModal$LoginFields$txtModalPassword'] = '';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$SearchForm$inputText'] = '';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = rawurlencode($key) . '=' . rawurlencode($value);
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//Set options for post
curl_setOpt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Origin: https://www.mysite.com', 'Host: www.mysite.com', 'Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_URL, $url);   
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setOpt($ch, CURLOPT_REFERER, 'https://www.mysite.com/account/login.htm');

// Perform our post request
$curl_scraped_page = curl_exec($ch);

echo $curl_scraped_page;

// Now get our account summary page
$urlAcctSummary = "https://www.mysite.com/my-account/summary.htm";
//Set options
curl_setOpt($ch, CURLOPT_HTTPGET, TRUE);
curl_setOpt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_URL, $urlAcctSummary);   
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); 

$curl_scraped_page = curl_exec($ch);

echo $curl_scraped_page;

curl_close($ch);

?>

解决方案

I figured it out. I tweaked the code in several ways, but I believe the root of my problem was that ASP.NET wants to set a session cookie from the very first GET request and I only specified the CURLOPT_COOKIEJAR on the POST request and CURLOPT_COOKIEFILE in the final GET request.

Once I put CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE in the very first GET request it worked as designed.

Here is what my code looks like after moving those around:

<?php

require_once  ("Includes/simple_html_dom.php");

ini_set('display_errors', 'On');
error_reporting(E_ALL);

// Create curl connection
$url = 'https://www.mysite.com/account/login.htm';
$cookieFile = 'cookie.txt';
$ch = curl_init();

// We must request the login page and get the ViewState and EventValidation hidden values
// and pass those along in the post request.

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setOpt($ch, CURLOPT_REFERER, 'https://www.mysite.com/account/login.htm');
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Origin: https://www.mysite.com', 'Host: www.mysite.com'));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);


$curl_scraped_page = curl_exec($ch);

// Grab ViewState and EventValidation data
$html = str_get_html($curl_scraped_page);
$viewState = $html->find("#__VIEWSTATE", 0);
$eventValidation = $html->find("#__EVENTVALIDATION", 0);
$previousPage = $html->find("#__PREVIOUSPAGE", 0);


//create array of data to be posted
// This matches exactly what I am seeing being posted when looking at Fiddler
$post_data['__EVENTTARGET'] = '';
$post_data['__EVENTARGUMENT'] = '';
$post_data['__VIEWSTATE'] = $viewState->value;
$post_data['__EVENTVALIDATION'] = $eventValidation->value;
$post_data['__PREVIOUSPAGE'] = $previousPage->value;
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$txtUsername'] = 'bsmith';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$txtPassword'] = 'Weez442';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$LoginFields$chkLoginPersist'] = 'on';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateContent$MyAccountLogin967$btnLogin'] = 'Login >';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$LoginModal$LoginFields$txtModalUsername'] = '';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$LoginModal$LoginFields$txtModalPassword'] = '';
$post_data['ctl00$ctl00$cphMasterBody$cphPageTemplateTopHeader$IncludeHeader$SearchForm$inputText'] = '';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = rawurlencode($key) . '=' . rawurlencode($value);
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//Set options for post
curl_setOpt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Origin: https://www.mysite.com', 'Host: www.mysite.com', 'Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_URL, $url);   
curl_setOpt($ch, CURLOPT_REFERER, 'https://www.mysite.com/account/login.htm');

// Perform our post request
$curl_scraped_page = curl_exec($ch);

echo $curl_scraped_page;

// Now get our account summary page
$urlAcctSummary = "https://www.mysite.com/my-account/summary.htm";
//Set options
curl_setOpt($ch, CURLOPT_HTTPGET, TRUE);
curl_setOpt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_URL, $urlAcctSummary);   
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$curl_scraped_page = curl_exec($ch);

echo $curl_scraped_page;

curl_close($ch);

?>

这篇关于如何使用PHP /卷曲发布ASP.NET登录表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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