简体   繁体   English

如何通过javascript提交的php curl登录,即表单中没有提交按钮

[英]how to login through php curl which is submited by javascript i.e no submit button in form

I am trying to login a secure https website thorugh curl . 我试图登录一个安全的https网站thorugh curl。 my code is running successfully for other sites but some website where form is submitting through javascript its not working . 我的代码在其他网站上已成功运行,但是某些通过javascript提交表单的网站无法正常工作。 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);
?>    

and the login for is as follows 的登录如下

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 mysql 从表单提交时存储图像名称/值(即与单选按钮相同) - How to store image name/value on submit in mysql from a form (i.e same as radio button) 仅当通过提交按钮以表单提交数据时,才启用对话框 - Enable Dialogbox only when data submited in a form through submit button 使用 javascript 事件侦听器单击表单提交按钮后未提交表单 - Form is not submited after form submit button click with javascript event listener 在表单验证并由ajax提交后,如何禁用或隐藏“提交”按钮? - How to disable or hide submit button after form validate and submited by ajax? 如何通过VueJS(Javascript)在php文件中提交表单? - How can I submit a form in a php file through VueJS (Javascript)? 使用PHP和JavaScript,“提交”按钮不适用于登录表单 - Submit button not working on login form, using PHP and JavaScript PHP到JavaScript变量无法在Chrome和IE中工作 - php to javascript variable not working in chrome and I.E JSF提交按钮 - 表单在一秒钟内被提交X次(直到视图重新创建) - 如何防止它? - JSF submit button - form is X times submited during one second (until view recreated) - how to prevent it? jQuery的步骤:提交表单时禁用提交按钮 - jquery-steps : disable submit button when form are submited 没有提交按钮的情况下如何向JavaScript提交表单? - How do I submit a form to JavaScript without the submit button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM