简体   繁体   English

使用file_get_contents发布登录-不卷曲

[英]Post login with file_get_contents - not curl

One site requires login for three variables: username, password and token. 一个站点需要登录三个变量:用户名,密码和令牌。 This data is sent via POST. 该数据通过POST发送。

token is also sent through a section. 令牌也通过一个部分发送。

I have all three variables, and I want to log in via file_get_contents. 我有所有三个变量,我想通过file_get_contents登录。

How can that data be sent and the authentication is successful? 如何发送数据并成功通过身份验证?

    function file_post_contents()
{

    $url = 'http://site.com/auth.php?' . session_name() . '=' . session_id();

    $login = 'jhon';
    $senha = 'doe';

    $token = '123456';  
    $_SESSION["token"] = $token;

    $postdata = http_build_query(
        array(
            'login' => $login,
            'senha' => $senha,
            'token' => $token, 
                            $_SESSION["token"] => $token
        )
    );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    if($login && $senha)
    {
        $opts['http']['header'] = ("Authorization: Basic " . base64_encode("$login:$senha"));
    }

    $context = stream_context_create($opts);
    return file_get_contents($url, false, $context);
}

Modified code. 修改后的代码。 It does not work. 这是行不通的。

    function file_post_contents()
{

    $url = 'http://site.com/auth.php?' . session_name() . '=' . session_id();

    $login = 'jhon';
    $senha = 'doe';

    $token = '123456';  
    $_SESSION["token"] = $token;

    $postdata = http_build_query(
        array(
            'login' => $login,
            'senha' => $senha,
            'token' => $token
            //, $_SESSION["token"] => $token
        )
    );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded'
             . "\r\n" . "Authorization: Basic " . base64_encode("$login:$senha"). "\r\n",
            'content' => $postdata
        )
    );


    $context = stream_context_create($opts);
    return file_get_contents($url, false, $context);
}

Your headers might be set wrong, try adding \\r\\n like so 您的标题可能设置错误,请尝试像这样添加\\r\\n

$opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'content' => $postdata
        )
    );

and later down the code 然后下代码

$opts['http']['header'] = $opts['http']['header'] . "Authorization: Basic " . base64_encode("$login:$senha") . "\\r\\n";

You're overriding $opts['http']['header'] with the Basic auth. 您正在使用基本身份验证覆盖$opts['http']['header'] You're not adding it to the Content-type, you're replacing Content-type with it. 您没有将其添加到Content-type中,而是用它替换了Content-type。

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

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