所以我尝试使用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。任何帮助将不胜感激。
答案 0 :(得分:2)
看起来您正在使用同一页面。因此,将不会填充$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。