简体   繁体   English

PHP发布数据以登录到外部网页

[英]PHP post data to login to external webpage

There is an external website i want to be able to login to from my website. 我希望有一个外部网站能够从我的网站登录。 the external website has this HTML: 外部网站具有以下HTML:

<form action="/Login" method="post">


            <div style="width: 280px; margin-left: auto; margin-right: auto; margin-top: 150px; padding: 10px 10px 10px 10px;" class="ui-corner-all">
                <div class="pw-input-group">
                    <span class="pw-input-group-addon">
                        <i class="fa fa-user fa-fw"></i>
                    </span>
                    <input id="username" name="username" class="required text pw-form-control" type="text" placeholder="Username" minlength="2" maxlength="45" />
                </div>
                <div class="pw-input-group">
                    <span class="pw-input-group-addon">
                        <i class="fa fa-key fa-fw"></i>
                    </span>
                    <input id="password" name="password" class="required text pw-form-control" type="password" placeholder="Password" minlength="2" maxlength="45" />
                </div>
                <div style="float: right;">
                    <input id="loginbutton" name="submit" style="line-height: 1.42857; padding: 6px 12px; font-size: 14px; color: #555;" type="submit" value="Login" />
                </div>
            </div>
        </form>

I have tried using this PHP code on my site: 我尝试在我的网站上使用此PHP代码:

//set POST variables
$url = 'https://portal.external-website.co.uk/Login';
$fields = array(
                'username' => urlencode("username"), 
                'password' => urlencode("password"), 
                'submit' => 'Login'
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) {
    $fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//we need to ignore SSL errors here
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//execute post
$result = curl_exec($ch);
var_dump($result);
echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';

//close connection
curl_close($ch);

It shows 表明

bool(true) Array
0

but when i visit the website again, its telling me im not logged in 但是当我再次访问该网站,它告诉我,我不是在登录

how can i get this to log me in? 我该如何登录?

Based on your comments, this should be the solution for fetching a page after logging in: 根据您的评论,这应该是登录后获取页面的解决方案:

$cookieFile = tempnam(__DIR__, "");
$loginUrl = 'https://portal.external-website.co.uk/Login';

$fields = array(
    'username' => "username", 
    'password' => "password", 
    'submit' => 'Login',
);
$fields_string = http_build_query($fields);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $loginUrl);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
$result = curl_exec($ch);
curl_close($ch);

$fetchUrl = "https://portal.external-website.co.uk/Profile/123";

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $fetchUrl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch); // This should be the sites content
curl_close($ch);

unlink($cookieFile);

There's a good chance the external website is keeping track of your logged-in state by placing a cookie, to store either a session reference or a token. 外部网站很有可能通过放置cookie来跟踪会话的登录状态,以存储会话参考或令牌。 Such a cookie can't be set when you're posting from someplace else than a browser. 从浏览器以外的其他地方发布时,无法设置此类cookie。

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

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