简体   繁体   English

卷曲发布用户名/密码登录

[英]Curl post username/password to login

I would like using Curl (or just any other method) to send login details to a login page. 我想使用Curl(或其他方法)将登录详细信息发送到登录页面。 Here's the code I'm using. 这是我正在使用的代码。

<?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>';

}

?>

But it keeps saying that the login details are wrong while they are not. 但它一直说登录详细信息有误,而没有。

Here's a demo with legitimate username/password: http://jaydz.me/test2/ 这是具有合法用户名/密码的演示: http : //jaydz.me/test2/

You need: 你需要:

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

EDIT: 编辑:

In your linked example the form you are trying to replicate with curl includes all parameters as hidden fields, ie, they all get sent as POST parameters. 在您的链接示例中,您尝试使用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);

If you can login via a browser to that page you should try using the chrome dev tools to inspect the request. 如果您可以通过浏览器登录到该页面,则应尝试使用chrome dev工具检查请求。 You should be able to see the exact POST data that is sent. 您应该能够看到发送的确切POST数据。

It may be the case that the service you are trying to POST to is rejecting your requests based on something else like the User Agent header. 可能是您尝试过POST的服务基于用户代理标头之类的其他东西拒绝了您的请求。 In which case your curl request should try and replicate the browser request as closely as possible. 在这种情况下,您的curl请求应该尝试并尽可能接近地复制浏览器请求。 Again, check all the headers being sent in chrome and add these to your curl request. 再次,检查所有以chrome发送的标头,并将其添加到curl请求中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM