0

所以我正在尝试使用 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。任何帮助将不胜感激。

4

1 回答 1

3

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

通常,在 codeignter 中,您会执行以下操作:

控制器:

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>

然后回到docusign控制器,添加处理表单的函数:

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。

于 2013-10-02T17:40:42.670 回答