使用curl php发送docx文件

时间:2013-11-19 13:12:36

标签: php docusignapi

我正在使用其余的api来进行docusign。

我正在向其他用户发送文件进行在线签名。代码中发生的事情是文档正在上传,并且正在使用file_get_contents函数读取文件的内容,然后使用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 = "\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=\”document.pdf\"; 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" )                                                                       
);

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

    $msg = 'Error. Please try again';

}

以上代码适用于pdf文件,但不适用于docx文件。

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

$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/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n"
."Content-Disposition: file; filename=\”document.docx\"; documentid=1 \r\n"
."\r\n"
."$file_contents\r\n"
."--myboundary--\r\n"
."\r\n";

由于

1 个答案:

答案 0 :(得分:0)

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

FWIW,我可以通过DocuSign REST 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--