使用curl php发送docx文件

2022-03-28 00:00:00 docusignapi php

我使用的是DocuSign睡觉接口

我正在将文档发送给另一个用户进行在线签名。代码中发生的情况是,正在上载文档,并使用file_get_content函数读取文件内容,然后使用cURL将此内容通过DocuSign发送给其他用户。

以下是我的代码:

$data = "{
  "emailBlurb":"",
  "emailSubject":"DocuSign API - Please Sign This Document...",
  "documents":[
    {
      "documentId":"1",
      "name":"agreement.pdf"
    }
  ],
  "recipients":{
    "signers":[
      {
        "email":"$email",
        "name":"$name",
        "recipientId":"1",
        "tabs":{
          "signHereTabs":[
            {
              "xPosition":"100",
              "yPosition":"100",
              "documentId":"1",
              "pageNumber":"1"
            }
          ]
        }
      }
    ]
  },
  "status":"sent"
}";  

$file_contents = file_get_contents("uploads/envelopes/" . $file_name);

$requestBody = "
"
."
"
."--myboundary
"
."Content-Type: application/json
"
."Content-Disposition: form-data
"
."
"
."$data
"
."--myboundary
"
."Content-Type:application/pdf
"
."Content-Disposition: file; filename="document.pdf"; documentid=1 
"
."
"
."$file_contents
"
."--myboundary--
"
."
";

// *** 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" )                                                                       
);

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {

    $msg = 'Error. Please try again';

}

以上代码对于pdf文件工作正常,但对于docx文件工作不正常。

对于docx文件,我这样更改了代码(更改了内容类型):

$requestBody = "
"
."
"
."--myboundary
"
."Content-Type: application/json
"
."Content-Disposition: form-data
"
."
"
."$data
"
."--myboundary
"
."Content-Type:application/vnd.openxmlformats-officedocument.wordprocessingml.document
"
."Content-Disposition: file; filename="document.docx"; documentid=1 
"
."
"
."$file_contents
"
."--myboundary--
"
."
";

谢谢


解决方案

DocuSign支持发送.docx文件,因此文件格式不会导致您的问题。当您尝试发送.docx文件时,是否收到特定的错误消息?如果是,那么错误是什么?

仅供参考,我可以通过DocuSign睡觉API发送.docx文件--我已经在下面包含了我的完整请求。如果可能,我建议您使用Fiddler(或任何类似的实用程序)来生成对发送到服务器的完整请求的跟踪,并将其内容/结构与我在下面包含的(成功的)示例请求进行比较。这样做有望使您能够识别(并解决)您的请求的问题。

POST https://demo.docusign.net/restapi/v2/accounts/ACCOUNT_NUMBER/envelopes HTTP/1.1

X-DocuSign-Authentication: {"Username":"USERNAME","Password":"PASSWORD","IntegratorKey":"INTEGRATOR_KEY"}
Content-Type: multipart/form-data; boundary=MY_BOUNDARY
Accept: application/json
Host: demo.docusign.net
Content-Length: 15384

--MY_BOUNDARY
Content-Type: application/json
Content-Disposition: form-data

{
    "status" : "sent",
    "emailBlurb":"Test Email Body",
    "emailSubject": "-- Test Email Subject --",
    "documents": [
    {
        "name": "CustomerAgreement.docx",
        "documentId": 1
    }],
    "recipients": {
        "signers" : [{
            "email": "bobsemail@outlook.com",
            "name": "Bob Adamson",
            "recipientId": "1",
            "routingOrder": "1",
            "tabs": {
                "signHereTabs": [
                {
                    "recipientId": "1",
                    "tabLabel": "Customer_Signature",
                    "documentId": "1",
                    "pageNumber": "1",
                    "xPosition": "99",
                    "yPosition": "424"
                }]
            }
        }]
    }
}

--MY_BOUNDARY
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Content-Disposition: file; filename="CustomerAgreement.docx"; documentid="1"

<document bytes removed>

--MY_BOUNDARY--

相关文章