-->

I'm getting status 0 when we are sending mail

2019-09-16 08:06发布

问题:

I am using sandbox account of DocuSign account. by using that I am sending mail with the help of API by using curl and PHP. But it is showing result like below.

[ok] => [errMsg] => Error calling DocuSign, status is: 0

And I am using the code below. to get the result.

define('DS_ACCOUNT_EMAIL',  'xyzmail@gmail.com');
define('DS_ACCOUNT_PW',     'xyzaccount');
define('DS_INTEGRATOR',     'YYPO-f28ccbb9-1cba-4f00-b03d-39814d76e700');
$documentName           = 'test';
$recipientEmail         = 'abcmail84@gmail.com';
$recipientName          = 'xyznamefortest';
$documentFileName       = 'New Text Document.txt';
 $opt =  request_signature_on_a_document($recipientEmail,$recipientName,$documentName,$documentFileName);
 print_r($opt);

function request_signature_on_a_document(
    $recipientEmail,  // signer's email
    $recipientName,   // signer's name -- first name last name
    $documentName,    // the "human" name for the document
    $documentFileName // including directory information
    ) {

    $email          = DS_ACCOUNT_EMAIL; // your account email.
    $password       = DS_ACCOUNT_PW;        // your account password
    $integratorKey  = DS_INTEGRATOR; // your account integrator key, found on (Preferences -> API page)
    // api service point
    $url            = "https://demo.docusign.net/restapi/v2/login_information"; // change for production

    $header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";

    $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 ) {
        return (['ok' => false, 'errMsg' => "Error calling DocuSign, status is: " . $status]);
    }
    $response = json_decode($json_response, true);
    $accountId = $response["loginAccounts"][0]["accountId"];
    $baseUrl = $response["loginAccounts"][0]["baseUrl"];
    curl_close($curl);

    $data = 
        array (
            "emailSubject" => "DocuSign API - Please sign " . $documentName,
            "documents" => array( 
                array("documentId" => "1", "name" => $documentName)
                ),
            "recipients" => array( 
                "signers" => array(
                    array(
                        "email" => $recipientEmail,
                        "name" => $recipientName,
                        "recipientId" => "1",
                        "tabs" => array(
                            "signHereTabs" => array(
                                array(
                                    "xPosition" => "100",
                                    "yPosition" => "100",
                                    "documentId" => "1",
                                    "pageNumber" => "1"
                                )
                            )
                        )
                    )
                )
            ),
        "status" => "sent"
    );
    $data_string    = json_encode($data);  
    $file_contents  = file_get_contents($documentFileName);
    // Create a multi-part request. First the form data, then the file content
    $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=\"$documentName\"; documentid=1 \r\n"
        ."\r\n"
        ."$file_contents\r\n"
        ."--myboundary--\r\n"
        ."\r\n";
    // Send to the /envelopes end point, which is relative to the baseUrl received above. 
    $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" )                                                                       
    );
    $json_response = curl_exec($curl) or die("err 116"); // Do it!
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ( $status != 201 ) {
    //  echo "Error calling DocuSign, status is:" . $status . "\nerror text: ";
        print_r($json_response); echo "\n";
        exit(-1);
    }
    $response = json_decode($json_response, true);
    $envelopeId = $response["envelopeId"];

    return [
                'ok' => true,
        'envelopeId' => $envelopeId,
         'accountId' => $accountId,
           'baseUrl' => $baseUrl
    ];

} // end of function request_signature_on_a_document

The purpose of this code is send mail. but it is always shows the status as 0. Is any thing i have to do with api, or setting of sandbox account.

回答1:

You are providing an incorrect file

$documentFileName = 'New Text Document.txt';

This should be Pdf file as you are specifying the Content-Type as application/pdf in the request body.

."--myboundary\r\n"
."Content-Type:application/pdf\r\n"

Also make sure the PDF file is present in your application directory. If not provide the complete path to the file.



标签: docusignapi