我刚刚测试并能够使用以下 PHP 代码将两个文档添加到草稿信封中。如果您输入您的凭据,此代码将登录,创建草稿信封,然后一次性将 2 个文档添加到该草稿信封中。
要运行此代码:
- 在本地保存文件。
- 在顶部输入凭据。
- 将两个 pdf 文件复制到同一目录,分别命名为 document1.pdf 和 document2.pdf。
- 运行代码。
注意:此调用的 REST API 文档有一些不准确之处,以下请求正文有效,因此请遵循此...
<?php
// Input your info here:
$integratorKey = '...';
$email = '...';
$password = '...';
$name = 'John Doe';
// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 1 - Login (to retrieve baseUrl and accountId)
/////////////////////////////////////////////////////////////////////////////////////////////////
$url = "https://demo.docusign.net/restapi/v2/login_information";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
echo "error calling webservice, status is:" . $status;
exit(-1);
}
$response = json_decode($json_response, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
curl_close($curl);
//--- display results
echo "\naccountId = " . $accountId . "\nbaseUrl = " . $baseUrl . "\n";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Create a draft envelope
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array (
"emailBlurb" => "This comes from PHP",
"emailSubject" => "DocuSign API Testing",
"status" => "created");
$data_string = json_encode($data);
// *** append "/envelopes" to baseUrl and as signature request endpoint
$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
return;
}
$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];
//--- display results
echo "Document is created! Envelope ID = " . $envelopeId . "\n\n";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 3 - Add documents to draft envelope
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array (
"documents" => array(
array( "documentId" => "1", "name" => "document1.pdf", "order" => "1" ),
array( "documentId" => "2", "name" => "document2.pdf", "order" => "2" )
));
$data_string = json_encode($data);
$file_contents1 = file_get_contents("document1.pdf");
$file_contents2 = file_get_contents("document2.pdf");
$requestBody = "\r\n"
."\r\n"
."--myboundary\r\n"
."Content-Type: application/json\r\n"
."Content-Disposition: form-data\r\n"
."\r\n"
."$data_string\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"document1.pdf\"; documentid=1 \r\n"
."\r\n"
."$file_contents1\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"document2.pdf\"; documentid=2 \r\n"
."\r\n"
."$file_contents2\r\n"
."--myboundary--\r\n"
."\r\n";
// *** append "/envelopes/{envelopeId}/documents" to baseUrl and as endpoint
$url = $baseUrl . "/envelopes/$envelopeId/documents";
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data;boundary=myboundary',
'Content-Length: ' . strlen($requestBody),
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($response); echo "\n";
exit(-1);
}
$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];
//--- display results
echo "Document is sent! Envelope ID = " . $envelopeId . "\n\n";
?>
我要注意的最后一件事是,确保您正在为此调用发出 PUT 请求而不是 POST,因为这是我在这里看到的常见错误。