我剛剛測試過,並且能夠使用以下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,因爲這是我在這裏看到的一個常見錯誤。
那麼你收到哪些錯誤信息? – Ergin