Search code examples
phpapiresthttpcurl

Validation error when making POST Boldsign API request


I hope you are all doing well.

I am trying to send a curl post request to the BoldSign API using their Send Document method (https://developers.boldsign.com/documents/send-document/). I have gotten the script to operate as intended when I hardcode the Signers Name or EmailAddress element as a string. The code breaks and I get a 400 bad request status code response when I try passing the required elements as a variable.

I have tried passing the variable without quotes, with single quotes, with double quotes and appended onto the end of an empty single quotes string. I thought that I may be having an issue with the formatting of post variables so I tried capturing the output of echo commands and print_r within an additional variable and passing that whilst trying the above variations. Unfortunately all attempts are resulting in an error. I will attach my full code snippet and the HTML form below. Thanks in advance for any and all assistance!

This is the error code I am getting:

{
    "errors": {
        "Signers[0].Name": [
            "The Name field is required."
        ]
    },
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-c4ebfc6a234153afba810315e2f4b37a-00373d37127c3c0b-00"
}

400

p.s the values being passed in the html form are formidable forms shortcodes containing the values of form fields.

HTML Form Code

    <form action="/wp-content/themes/hello-theme-child-master/send-document-short.php" method="post" enctype="multipart/form-data">
        staff name:
            <input type="text" id="staffName" name="staffName" value="[7394]"><br>
        staff email
            <input type="text" id="staffEmail" name="staffEmail" value="[7397]"><br>
        client name:
            <input type="text" id="clientName" name="clientName" value="[7396]"><br>
        client email
            <input type="text" id="clientEmail" name="clientEmail" value="[7398]" ><br>
        Select pdf to upload:
            <input type="file" name="contractFile" id="contractFile" required><br>
            <input type="submit" value="Send File" name="submit">
    </form>

PHP Script Code

<?php

    if ($_FILES['contractFile']['error'] !== UPLOAD_ERR_OK) {
        die("File upload failed with error code " . $_FILES['contractFile']['error']);
    }

    print_r($_FILES[contractFile]);
    $staffName = $_POST['staffName'];
    $staffEmail = $_POST['staffEmail'];
    $clientName = $_POST['clientName'];
    $clientEmail = $_POST['clientEmail'];
    $file = $_FILES['contractFile'];

    $sName = print_r($staffName);
    $sEmail = print_r($staffEmail);
    $cName = print_r($clientName);
    $cEmail = print_r($clientEmail);

    runSend($file);

    function runSend($file){

        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, 'https://api.boldsign.com/v1/document/send');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        
        $post = array(
            'DisableExpiryAlert' => 'false',
            'ReminderSettings.ReminderDays' => '5',
            'BrandId' => '',
            'ReminderSettings.ReminderCount' => '3',
            'EnableReassign' => 'false',
            'Message' => '',
            'Signers[0][Name]' => $cName,
            'Signers[0][EmailAddress]' => '[email protected]',
            'Signers[0][SignerOrder]' => '0',

            'Signers[0][FormFields][0][Type]' => 'Initial',
            'Signers[0][FormFields][0][name]' => 'initial00',
            'Signers[0][FormFields][0][pageNumber]' => '3',
            'Signers[0][FormFields][0][Bounds][X]' => '690',
            'Signers[0][FormFields][0][Bounds][Y]' => '1065',
            'Signers[0][FormFields][0][Bounds][Width]' => '25',
            'Signers[0][FormFields][0][Bounds][Height]' => '25',
            'Signers[0][FormFields][0][IsRequired]' => 'true',
            

            'Signers[0][FormFields][1][Type]' => 'Initial',
            'Signers[0][FormFields][1][Name]' => 'initial01',
            'Signers[0][FormFields][1][PageNumber]' => '4',
            'Signers[0][FormFields][1][Bounds][X]' => '690',
            'Signers[0][FormFields][1][Bounds][Y]' => '1065',
            'Signers[0][FormFields][1][Bounds][Width]' => '25',
            'Signers[0][FormFields][1][Bounds][Height]' => '25',
            'Signers[0][FormFields][1][IsRequired]' => 'true',

            'Signers[0][FormFields][2][Type]' => 'Initial',
            'Signers[0][FormFields][2][Name]' => 'initial02',
            'Signers[0][FormFields][2][PageNumber]' => '5',
            'Signers[0][FormFields][2][Bounds][X]' => '690',
            'Signers[0][FormFields][2][Bounds][Y]' => '1065',
            'Signers[0][FormFields][2][Bounds][Width]' => '25',
            'Signers[0][FormFields][2][Bounds][Height]' => '25',
            'Signers[0][FormFields][2][IsRequired]' => 'true',

            'Signers[0][FormFields][3][Type]' => 'Initial',
            'Signers[0][FormFields][3][Name]' => 'initial03',
            'Signers[0][FormFields][3][PageNumber]' => '6',
            'Signers[0][FormFields][3][Bounds][X]' => '690',
            'Signers[0][FormFields][3][Bounds][Y]' => '1065',
            'Signers[0][FormFields][3][Bounds][Width]' => '25',
            'Signers[0][FormFields][3][Bounds][Height]' => '25',
            'Signers[0][FormFields][3][IsRequired]' => 'true',

            'Signers[0][FormFields][4][Type]' => 'Initial',
            'Signers[0][FormFields][4][Name]' => 'initial04',
            'Signers[0][FormFields][4][PageNumber]' => '7',
            'Signers[0][FormFields][4][Bounds][X]' => '690',
            'Signers[0][FormFields][4][Bounds][Y]' => '1065',
            'Signers[0][FormFields][4][Bounds][Width]' => '25',
            'Signers[0][FormFields][4][Bounds][Height]' => '25',
            'Signers[0][FormFields][4][IsRequired]' => 'true',

            'Signers[0][FormFields][5][Type]' => 'Initial',
            'Signers[0][FormFields][5][Name]' => 'initial05',
            'Signers[0][FormFields][5][PageNumber]' => '8',
            'Signers[0][FormFields][5][Bounds][X]' => '690',
            'Signers[0][FormFields][5][Bounds][Y]' => '1065',
            'Signers[0][FormFields][5][Bounds][Width]' => '25',
            'Signers[0][FormFields][5][Bounds][Height]' => '25',
            'Signers[0][FormFields][5][IsRequired]' => 'true',

            'Signers[0][FormFields][6][Type]' => 'Initial',
            'Signers[0][FormFields][6][Name]' => 'initial06',
            'Signers[0][FormFields][6][PageNumber]' => '9',
            'Signers[0][FormFields][6][Bounds][X]' => '690',
            'Signers[0][FormFields][6][Bounds][Y]' => '1065',
            'Signers[0][FormFields][6][Bounds][Width]' => '25',
            'Signers[0][FormFields][6][Bounds][Height]' => '25',
            'Signers[0][FormFields][6][IsRequired]' => 'true',

            'Signers[0][FormFields][7][Type]' => 'Initial',
            'Signers[0][FormFields][7][Name]' => 'initial07',
            'Signers[0][FormFields][7][PageNumber]' => '10',
            'Signers[0][FormFields][7][Bounds][X]' => '690',
            'Signers[0][FormFields][7][Bounds][Y]' => '1065',
            'Signers[0][FormFields][7][Bounds][Width]' => '25',
            'Signers[0][FormFields][7][Bounds][Height]' => '25',
            'Signers[0][FormFields][7][IsRequired]' => 'true',

            'Signers[0][FormFields][8][Type]' => 'Initial',
            'Signers[0][FormFields][8][Name]' => 'initial08',
            'Signers[0][FormFields][8][PageNumber]' => '11',
            'Signers[0][FormFields][8][Bounds][X]' => '690',
            'Signers[0][FormFields][8][Bounds][Y]' => '1065',
            'Signers[0][FormFields][8][Bounds][Width]' => '25',
            'Signers[0][FormFields][8][Bounds][Height]' => '25',
            'Signers[0][FormFields][8][IsRequired]' => 'true',

            'Signers[0][FormFields][9][Type]' => 'Initial',
            'Signers[0][FormFields][9][Name]' => 'initial09',
            'Signers[0][FormFields][9][PageNumber]' => '12',
            'Signers[0][FormFields][9][Bounds][X]' => '690',
            'Signers[0][FormFields][9][Bounds][Y]' => '1065',
            'Signers[0][FormFields][9][Bounds][Width]' => '25',
            'Signers[0][FormFields][9][Bounds][Height]' => '25',
            'Signers[0][FormFields][9][IsRequired]' => 'true',

            'Signers[0][FormFields][10][Type]' => 'Initial',
            'Signers[0][FormFields][10][Name]' => 'initial010',
            'Signers[0][FormFields][10][PageNumber]' => '13',
            'Signers[0][FormFields][10][Bounds][X]' => '690',
            'Signers[0][FormFields][10][Bounds][Y]' => '1065',
            'Signers[0][FormFields][10][Bounds][Width]' => '25',
            'Signers[0][FormFields][10][Bounds][Height]' => '25',
            'Signers[0][FormFields][10][IsRequired]' => 'true',

            'Signers[0][FormFields][11][Type]' => 'Initial',
            'Signers[0][FormFields][11][Name]' => 'initial011',
            'Signers[0][FormFields][11][PageNumber]' => '14',
            'Signers[0][FormFields][11][Bounds][X]' => '690',
            'Signers[0][FormFields][11][Bounds][Y]' => '1065',
            'Signers[0][FormFields][11][Bounds][Width]' => '25',
            'Signers[0][FormFields][11][Bounds][Height]' => '25',
            'Signers[0][FormFields][11][IsRequired]' => 'true',

            'Signers[0][FormFields][12][Type]' => 'Signature',
            'Signers[0][FormFields][12][Name]' => 'signature00',
            'Signers[0][FormFields][12][PageNumber]' => '8',
            'Signers[0][FormFields][12][Bounds][X]' => '80',
            'Signers[0][FormFields][12][Bounds][Y]' => '760',
            'Signers[0][FormFields][12][Bounds][Width]' => '205',
            'Signers[0][FormFields][12][Bounds][Height]' => '25',
            'Signers[0][FormFields][12][IsRequired]' => 'true',

            'Signers[0][FormFields][13][Type]' => 'DateSigned',
            'Signers[0][FormFields][13][Name]' => 'datesign00',
            'Signers[0][FormFields][13][PageNumber]' => '8',
            'Signers[0][FormFields][13][Bounds][X]' => '390',
            'Signers[0][FormFields][13][Bounds][Y]' => '760',
            'Signers[0][FormFields][13][Bounds][Width]' => '150',
            'Signers[0][FormFields][13][Bounds][Height]' => '25',
            'Signers[0][FormFields][13][IsRequired]' => 'true',

            'Signers[0][FormFields][14][Type]' => 'Signature',
            'Signers[0][FormFields][14][Name]' => 'signature01',
            'Signers[0][FormFields][14][PageNumber]' => '12',
            'Signers[0][FormFields][14][Bounds][X]' => '370',
            'Signers[0][FormFields][14][Bounds][Y]' => '820',
            'Signers[0][FormFields][14][Bounds][Width]' => '180',
            'Signers[0][FormFields][14][Bounds][Height]' => '25',
            'Signers[0][FormFields][14][IsRequired]' => 'true',

            'Signers[0][FormFields][15][Type]' => 'DateSigned',
            'Signers[0][FormFields][15][Name]' => 'datesign01',
            'Signers[0][FormFields][15][PageNumber]' => '12',
            'Signers[0][FormFields][15][Bounds][X]' => '600',
            'Signers[0][FormFields][15][Bounds][Y]' => '820',
            'Signers[0][FormFields][15][Bounds][Width]' => '120',
            'Signers[0][FormFields][15][Bounds][Height]' => '25',
            'Signers[0][FormFields][15][IsRequired]' => 'true',

            'Signers[1][Name]' => 'Alexander Name',
            'Signers[1][EmailAddress]' => '[email protected]',
            'Signers[1][SignerOrder]' => '0',

            'Signers[1][FormFields][0][Type]' => 'Signature',
            'Signers[1][FormFields][0][Name]' => 'signature10',
            'Signers[1][FormFields][0][PageNumber]' => '8',
            'Signers[1][FormFields][0][Bounds][X]' => '90',
            'Signers[1][FormFields][0][Bounds][Y]' => '832',
            'Signers[1][FormFields][0][Bounds][Width]' => '200',
            'Signers[1][FormFields][0][Bounds][Height]' => '20',
            'Signers[1][FormFields][0][IsRequired]' => 'true',

            'Signers[1][FormFields][1][Type]' => 'DateSigned',
            'Signers[1][FormFields][1][Name]' => 'datesign10',
            'Signers[1][FormFields][1][PageNumber]' => '8',
            'Signers[1][FormFields][1][Bounds][X]' => '390',
            'Signers[1][FormFields][1][Bounds][Y]' => '832',
            'Signers[1][FormFields][1][Bounds][Width]' => '100',
            'Signers[1][FormFields][1][Bounds][Height]' => '30',
            'Signers[1][FormFields][1][IsRequired]' => 'true',

            'ExpiryDays' => '30',
            'EnablePrintAndSign' => 'false',
            'AutoDetectFields' => 'false',
            'OnBehalfOf' => '',
            'EnableSigningOrder' => 'false',
            'UseTextTags' => 'true',
            'SendLinkValidTill' => '',
            'Files' => new CURLFILE($file['tmp_name'], $file['type'], $file['name']),
            'Title' => 'testing',
            'HideDocumentId' => 'false',
            'EnableEmbeddedSigning' => 'false',
            'ExpiryDateType' => 'Days',
            'ReminderSettings.EnableAutoReminder' => 'true',
            'ExpiryValue' => '60',
            'DisableEmails' => 'false'
        );
        
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

        $headers = array();
        $headers[] = 'Accept: application/json';
        $headers[] = 'X-API-Key: APRETENDAPIKEY4124234';
        $headers[] = 'Content-Type: multipart/form-data';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        
        echo $result;
        
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        echo $httpcode;
        
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);

    };
?>


Solution

  • Variable Scope Issue

    PHP is a local variable scope language. The variable $cName is not accessible within your function runSend() and therefore consider NULL in value. That would make Signers[0].Name either NULL or an empty string when submitted. Hence the error.

    The simplest fix is to pass the variables, by value, into the function:

    runSend($file, $sName, $sEmail, $cName, $cEmail);
    
    function runSend($file, $sName, $sEmail, $cName, $cEmail){
        // ...
    }
    

    Usage: print_r

    Also, why use print_r() for string assignment? print_r() only returns boolean true unless you set the second flag ($return). Try these:

    var_dump(print_r('hello'));
    var_dump(print_r('hello', true));
    

    Result:

    hellobool(true)
    string(5) "hello"
    

    I guess you want the second one, right? But why bother with print_r() while you can simply do:

    $sName = $staffName;
    $sEmail = $staffEmail;
    $cName = $clientName;
    $cEmail = $clientEmail;
    

    or even

    runSend($file, $staffName, $staffEmail, $clientName, $clientEmail);
    
    function runSend($file, $sName, $sEmail, $cName, $cEmail){
        // ...
    }