0

I am using the DocuSign PHP Rest API. I have copied one of the quick-start examples, however, it is throwing a HTTP 500 Error. I am very new too this API so any help would be appreciated on what could be causing this error. Nothing shows in the browser console and I can't seem to find if there is any syntax errors in the code.

I would imagine that it isn't finding the API properly?

Here is the sign-request.php

<?php

require_once("vendors/docusign/autoload.php");

function send_document_for_signing(){
    $accessToken = '***hidden***';
    $accountId = '***hidden***';

    $signerName = 'John Doe';
    $signerEmail = 'John.Doe@example.com';
    
    $fileNamePath = 'demo/demo_orderform.pdf';

    $basePath = 'https://demo.docusign.net/restapi';

    # Constants
    $appPath = getcwd();

    #
    # Step 1. The envelope definition is created.
    #         One signHere tab is added.
    #         The document path supplied is relative to the working directory
    #
    # Create the component objects for the envelope definition...
    $contentBytes = file_get_contents($appPath . "/" . $fileNamePath);
    $base64FileContent =  base64_encode ($contentBytes);

    # create the DocuSign document object
    $document = new DocuSign\eSign\Model\Document([  
        'document_base64' => $base64FileContent, 
        'name' => 'Example document', # can be different from actual file name
        'file_extension' => 'pdf', # many different document types are accepted
        'document_id' => '1' # a label used to reference the doc
    ]);
    
    # The signer object
    $signer = new DocuSign\eSign\Model\Signer([ 
        'email' => $signerEmail, 'name' => $signerName, 'recipient_id' => "1", 'routing_order' => "1"
    ]);

    # DocuSign SignHere field/tab object
    $signHere = new DocuSign\eSign\Model\SignHere([ 
        'document_id' => '1', 'page_number' => '1', 'recipient_id' => '1', 
        'tab_label' => 'SignHereTab', 'x_position' => '195', 'y_position' => '147'
    ]);

    # Add the tabs to the signer object
    # The Tabs object wants arrays of the different field/tab types
    $signer->setTabs(new DocuSign\eSign\Model\Tabs(['sign_here_tabs' => [$signHere]])); 

    # Next, create the top level envelope definition and populate it.
    $envelopeDefinition = new DocuSign\eSign\Model\EnvelopeDefinition([
        'email_subject' => "Please sign this document",
        'documents' => [$document], # The order in the docs array determines the order in the envelope
        # The Recipients object wants arrays for each recipient type
        'recipients' => new DocuSign\eSign\Model\Recipients(['signers' => [$signer]]), 
        'status' => "sent" # requests that the envelope be created and sent.
    ]);
    
    #
    #  Step 2. Create/send the envelope.
    #
    $config = new DocuSign\eSign\Configuration();
    $config->setHost($basePath);
    $config->addDefaultHeader("Authorization", "Bearer " . $accessToken);
    $apiClient = new DocuSign\eSign\Client\ApiClient($config);
    $envelopeApi = new DocuSign\eSign\Api\EnvelopesApi($apiClient);
    $results = $envelopeApi->createEnvelope($accountId, $envelopeDefinition);
    return $results;
};

# Mainline
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        $results = send_document_for_signing();
?>
<html lang="en">
    <body>
    <h4>Results</h4>
    <p>Status: <?php $results['status'] ?>, Envelope ID: <?php $results['envelope_id'] ?></p>
    </body>
</html>
        <?php
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
        if ($e instanceof DocuSign\eSign\Client\ApiException) {
            print ("\nDocuSign API error information: \n");
            var_dump ($e->getResponseBody());
        }
    }    
    die();
}
# Since it isn't a POST, print the form:
?>
<html lang="en">
    <body>
        <form method="post">
            <input type="submit" value="Send document signature request!"
                style="width:21em;height:2em;background:#1f32bb;color:white;font:bold 1.5em arial;margin: 3em;"/>
        </form>
    </body>
</html>

I have hidden the OAuth Token, Account ID and true Sign Name and Email for security reasons.

Any assistance would be handy.

Many thanks, Nathan

  • 1
    _“it is throwing a HTTP 500 Error”_ - what is - the API itself, or _your_ page? In case it is the latter - go check what the error log has to say. – CBroe Aug 19 '20 at 14:02
  • 1
    A 500 error is a generic error message and covers pretty much every single thing that can go wrong with a PHP script. Check your server error logs to find out the exact error message. – aynber Aug 19 '20 at 14:04
  • If the error is coming back from DocuSign, a 500 error typically means there's something wrong with the overall call body or envelope process. The reason itself can vary -- in the response you received back from our system you should also have an X-DocuSign-TraceToken that will give us a reference point to track down the error message. We can often track this down via an accountId or envelopeId, which in this case you probably only have the former. Send me an email at matt.king@docusign.com with said tracetoken and/or accountId and we can have a closer look to see what's going on. – Matt King DS Aug 19 '20 at 19:38

0 Answers0