使用登录表单进行 HTTP 身份验证


HTTP Authentication using login form

所以我正在尝试将REST和cURL与PHP(Codeigniter)一起使用,从我们的自定义界面登录到我们的DocuSign开发帐户。这是我到目前为止的代码:

<?php
    $integratorKey = '****-********-****-****-****-************';
    $username = '';
    $password = '';
    $header = "<DocuSignCredentials><Username>" . $username . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";
    $url = "https://demo.docusign.net/restapi/v2/login_information";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, array("Content-Type: application/xml"));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
    curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
    $output = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ( $status != 200 ) {
        echo "error calling webservice, status is:" . $status;
        exit(-1);
    }
    $response = json_decode($output, true);
    $accountId = $response["loginAccounts"][0]["accountId"];
    $baseUrl = $response["loginAccounts"][0]["baseUrl"];
    curl_close($ch);
?>
<div id="container">
    <h1>Welcome</h1>
    <div id="login_form">
        <h1>Login</h1>
        <?php
            echo form_open('login/hello');
            echo form_input($username, 'Username');
            echo form_password($password, 'Password');
            echo form_submit('submit', 'Submit');
        ?>
    </div>
</div>

我得到的错误是 400。我意识到这可能是因为它在用户输入用户名和密码之前发送了标头。我想我不确定如何在发送标头请求之前让它"等待",直到按下提交按钮。这是我第一次涉足REST和cURL。任何帮助将不胜感激。

看起来您正在同一页面上工作。因此,不会填充$header变量,并且请求将失败。

通常,在代码ignter中,你会执行以下操作:

控制器:

class Docusign extends CI_Controller{
    function index(){
        $this->load->view('docusign_auth');
    }
}

景观 (docusign_auth_view.php)

<div id="container">
    <h1>Welcome</h1>
    <div id="login_form">
        <h1>Login</h1>
        <?php
            // have to add the target url here in form_open
            // you also won't have values to populate the fields with
            // so no need for the $Username/$Password variables 
            // which wouldn't work like that anyway since the field name
            // goes first, not the value
            echo form_open('docusign/authenticate');
            echo form_input('username');
            echo form_password('password');
            echo form_submit('submit', 'Submit');
        ?>
    </div>
</div>

然后回到文档编辑器中,添加函数来处理表单:

class Docusign extends CI_Controller{
    function index(){
        $this->load->view('docusign_login');
    }
    function authenticate(){
        $post = $this->input->post();
        extract($post);
        $integratorKey = '****-********-****-****-****-************';
        $header = "<DocuSignCredentials><Username>" . $username . "</Username><Password>" .     $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";
        $url = "https://demo.docusign.net/restapi/v2/login_information";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, array("Content-Type: application/xml"));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
        curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
        $output = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ( $status != 200 ) {
            echo "error calling webservice, status is:" . $status;
            exit(-1);
        }
        $response = json_decode($output, true);
        $accountId = $response["loginAccounts"][0]["accountId"];
        $baseUrl = $response["loginAccounts"][0]["baseUrl"];
        curl_close($ch);
}

以便将表单中填充的值发送到控制器方法,然后控制器方法可以将它们放入 cURL 请求中。

要登录,您将访问http://example.com/index.php/docusign,它将加载docusign_auth_view.php其中有一个表单将提交给http://example.com/index.php/docusign/authenticate其中表单中的变量将被填充到 cURL 请求中以发送到 DocuSign。