卷发用户名/密码登录

时间:2015-06-14 10:07:11

标签: php post curl login external

我想使用Curl(或任何其他方法)将登录详细信息发送到登录页面。这是我正在使用的代码。

<?php
$username='myMAIL/USERNAMEhere'; 
$password='myPASSWORDhere'; 
$postdata = 'username='.$username.'&password='.$password;

// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL,     'https://secure.runescape.com/m=weblogin/loginform.ws?        mod=www&ssl=1&expired=0&dest=account_settings.ws?jptg=ia&jptv=navbar');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// CLOSE CURL
curl_close ($ch); 

if ($store=='') {

//username and password are valid
//if login is valid, hotfile website returns nothing
echo '<br><i>Login Works!! Enjoy.</i>';

} else {

//username and password are not valid
//if username or password is invalid, the website returns 
//invalid username or password page
echo '<br><i>Login does not work.</i>';

}

?>

但它一直说登录细节是错误的,而不是。

这是一个包含合法用户名/密码的演示: http://jaydz.me/test2/

1 个答案:

答案 0 :(得分:1)

你需要:

$postdata = 'username='.$username.'&password='.$password;

编辑:

在您的链接示例中,您尝试使用curl复制的表单将所有参数包含为隐藏字段,即它们都将作为POST参数发送。

$fields = array(
    'username='. urlencode($username),
    'password=' . urlencode($password),
    'mod=www',
    'ssl=1',
    'expired=0',
    'dest=' . urlencode('account_settings.ws?jptg=ia&jptv=navbar')
);
$postdata = implode('&', $fields);

如果您可以通过浏览器登录该页面,则应尝试使用chrome dev工具检查请求。您应该能够看到发送的确切POST数据。

可能的情况是,您尝试POST的服务会根据用户代理标头等其他内容拒绝您的请求。在这种情况下,您的curl请求应尽可能地尝试复制浏览器请求。再次,检查chrome中发送的所有标题,并将其添加到curl请求中。

相关问题