[英]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.