Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// Input your info here:
$integratorKey = '***'; // found on Preferences -> API page
$username = '***'; // userId or email address
$password = '***'; // member password
$recipientName = '***'; // recipient (signer) name
$recipientEmail = '***'; // recipient (signer) email
$documentName = '***'; // copy doc with same name into this directory
// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $username . "</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 an envelope with one document, one recipient, one tab and send!
/////////////////////////////////////////////////////////////////////////////////////////////////
// The following JSON request body will place a signature tab 100 pixels to the right and
// 150 pixels down from the top left corner of the document, as an example. You can also
// position tabs through Templates and or Anchor Tagging
//
// Templates - https://www.docusign.com/developer-center/explore/features/templates
// Anchor Tagging - https://www.docusign.com/developer-center/explore/features/stick-etabs
//
$data = "{
\"emailSubject\":\"DocuSign API Sample - Please Sign This Document!\",
\"documents\":[
{
\"documentId\":\"1\",
\"name\":\"$documentName\"
}
],
\"recipients\":{
\"signers\":[
{
\"email\":\"$recipientEmail\",
\"name\":\"$recipientName\",
\"recipientId\":\"1\",
\"tabs\":{
\"signHereTabs\":[
{
\"xPosition\":\"100\",
\"yPosition\":\"150\",
\"documentId\":\"1\",
\"pageNumber\":\"1\"
}
]
}
}
]
},
\"status\":\"sent\"
}";
// read contents of the test document you copied into same directory
$file_contents = file_get_contents($documentName);
$requestBody = "\r\n"
."\r\n"
."--myboundary\r\n"
."Content-Type: application/json\r\n"
."Content-Disposition: form-data\r\n"
."\r\n"
."$data\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"$documentName\"; documentid=1 \r\n"
."\r\n"
."$file_contents\r\n"
."--myboundary--\r\n"
."\r\n";
// *** 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, $requestBody);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data;boundary=myboundary',
'Content-Length: ' . strlen($requestBody),
"X-DocuSign-Authentication: $header" )
);
// parse the response
$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";
exit(-1);
}
$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];
//--- display results
echo "Document has been sent to $recipientEmail! Envelope ID is " . $envelopeId . "\n\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment