我正在尝试使用php curl登录网站。
网站HTML的一部分如下所示:
<!-- div class="loginbox" -->
<div class="content">
<h1>Login</h1>
<p>Welcome</p>
<div class="loginbox">
<div class="leftpane">
<form action="/idp/profile/SAML2/Redirect/SSO;jsessionid=62CDB993EDA470442EE3DF03580F7EEA?execution=e1s1" method="post">
<h2>Login</h2>
<p>Please log in with username and password</p>
<table>
<tr><td width="40%"><label for="username">Username:</label></td><td><input name="j_username" type="text" id="username" autocapitalize="off" /></td></tr>
<tr><td><label for="password">Password:</label></td><td><input name="j_password" type="password" id="password" /></td></tr>
<tr><td></td><td><button class="form-element form-button" type="submit" name="_eventId_proceed">Login</button></td></tr>
</table>
我当前的PHP脚本如下:
<?php
//based on: http://thisinterestsme.com/php-login-to-website-with-curl/
define('USERNAME', 'myUserName');
define('PASSWORD', 'myPassword');
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
define('COOKIE_FILE', 'cookie.txt');
define('START_URL', 'https://url-before-redirect');
$postValues = array(
'j_username' => USERNAME,
'j_password' => PASSWORD
);
//Initiate cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, START_URL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_USERAGENT, USER_AGENT);
$a = curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // last effective URL
//URL of the login form.
define('LOGIN_FORM_URL', $url);
//action-URL of the buttons
define('LOGIN_ACTION_URL', $url);
curl_setopt($ch, CURLOPT_URL, LOGIN_ACTION_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($ch, CURLOPT_REFERER, LOGIN_FORM_URL);
//Execute the login request.
curl_exec($ch);
//Check for errors!
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
//We should be logged in by now. Let's attempt to access a password protected page
curl_setopt($ch, CURLOPT_URL, 'https://some.page.com');
//Execute the GET request and print out the result.
echo curl_exec($ch);
?>
执行此脚本后,curl似乎再次出现在登录屏幕上。
我尝试使用firefox提供的开发人员工具(F12)遵循登录过程。这就是POST请求的正文:
RelayState = cookie%3A1540823493_27cc& SAMLResponse = PD94bWwgdmVyc2lvbj0iM ...
这是我的期望:
j_username = myUserName
j_password = myPassword
任何人都可以给我一些有关如何解决此问题的提示吗?