如何通过PHP提交的javascript curc curl,即没有提交按钮在窗体中登录 [英] how to login through php curl which is submited by javascript i.e no submit button in form

查看:225
本文介绍了如何通过PHP提交的javascript curc curl,即没有提交按钮在窗体中登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试登录安全的https网站。我的代码运行成功为其他网站,但某些网站,窗体通过javascript提交它不工作。目前我使用下面的代码curl

I am trying to login a secure https website thorugh curl . my code is running successfully for other sites but some website where form is submitting through javascript its not working . currently i am using the following code for curl

<?
# Define target page
$target = "https://www.domainname.com/login.jsf";
# Define the login form data
$form_data="enter=Enter&username=webbot&password=sp1der3";
# Create the cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target); // Define target site
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return page in string
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // Tell cURL where to write cookies
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt"); // Tell cURL which cookies to send
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
# Execute the PHP/CURL session and echo the downloaded page
$page = curl_exec($ch);
echo $page;
# Close the cURL session
curl_close($ch);
?>    

,登录名如下

<form name="login" method="post" action="./servelet" >
Username=<input type="text" name="username" value="" >
Password=<input type="text" name="password" value="" >

<a href="javascript:void(0);" onclick="return submitfrm();">Submit</a>
<form>    

请帮助解决此问题。如何登录这些类型的表单。提前致谢。
此代码适用于从提交按钮提交的其他登录表单。

Please help to solve this issue . how to login these type of forms. Thanks in advance. This code is working for other login form which is submit from submit button.

推荐答案

$EMAIL            = '';
$PASSWORD         = '!';

$cookie_file_path = "/tmp/cookies.txt";
$LOGINURL         = "/login.jsf"; 
$agent            = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1";


// begin script
$ch = curl_init(); 

// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";

// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_HEADER,  0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);         
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

// set first URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);

// execute session to get cookies and required form inputs
$content = curl_exec($ch); 

 // grab the hidden inputs from the form required to login
$fields = getFormFields($content);
$fields['loginForm:userName'] = $EMAIL;
$fields['loginForm:password'] = $PASSWORD;

// get x value that is used in the login url
$x = '';
if (preg_match('/login\.jsf/i', $content, $match)) {
echo $x = $match[1];
}

//$LOGINURL   = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
   $LOGINURL   = "/pages/login.jsf";

// set postfields using what we extracted from the form
   $POSTFIELDS = http_build_query($fields); 

// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL); 

// set post options
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); 

// perform login
 $result = curl_exec($ch);  

 //print $result; 
$url2='';
curl_setopt($ch, CURLOPT_URL, $url2); 

$result2 = curl_exec($ch);  


function getFormFields($data)
{
if (preg_match('/(<form id="loginForm.*?<\/form>)/is', $data, $matches)) {
    $inputs = getInputs($matches[1]);

    return $inputs;
} else {
    die('didnt find login form');
}
}

这篇关于如何通过PHP提交的javascript curc curl,即没有提交按钮在窗体中登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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