在php中通过CURL发送base64编码文件沿着JSON

n53p2ov0  于 2022-12-02  发布在  PHP
关注(0)|答案(1)|浏览(344)

我尝试使用CURL通过其从文件创建文档API调用将文件发送到PANDADOCS:https://developers.pandadoc.com/reference/create-document-from-pdf
除了发送文件外,我还需要将包含收件人等信息的数据对象作为JSON作为multipart/form-data字符串的一部分发送。我不确定如何正确设置此调用,并且不断收到API返回的各种错误消息,如“There is field called file”(有一个名为file的字段)
以下是我目前掌握的情况:

public function createDocument()
    {

        $p = getmypid();
        $m = "({$p}): PandaDoc::create document: ";

        $postfields         = array();
        $postfields['name'] = $this->document->name;
        $postfields['file']  = $this->document->file; //base 64 encoded PDF

        $recipients = array(
            array(
                'email'      => 'a.mcdoogle@test.com',
                'first_name' => 'Andrew',
                'last_name'  => 'Mcdoogle',
                'role'       => 'user',
                'signing_order' => 1
            )
        );

        $data = array();
        $data['recipients'] = $recipients;

        $owner = array(
            "email" => "john@example.com"
        );

        $data['owner'] = $owner;

        $postfields['data'] =  json_encode($data);

        $header = array("Authorization: API-Key {$this->api_key}", "Content-Type: multipart/form-data", "accept" => "application/json");

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

        $res = curl_exec($ch);

        if ($res === false) {
            $errno = curl_errno($ch);
            $error = curl_error($ch);
            error_log("{$m}cURL error: {$error} ({$errno})");
            throw new Exception("{$m}cURL error: {$error} ({$errno})");
        }

        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close ($ch);

        error_log("{$m}Results from PandaDoc: {$res}");

        $response = json_decode($res);

        return $response;

    }

谁能告诉我我做错了什么?

bf1o4zei

bf1o4zei1#

这是不对的:

$postfields['data'] =  json_encode($data);

我发现他们的API文档令人不安。
简短教程:
下面是一个简单的多部分/表单数据HTML表单:
下面我把这个形式翻译成curl。

<form action="<url>" method="post" enctype="multipart/form-data">

<input type="text" name="name1" value="value1" >
<input type="text" name="name2" value="value2" >
<input type="text" name="name3" value="value3" >

<button type="submit">submit</button>
</form>

要在curl中发送此消息,必须将form-data放在postfield中。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '<url>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "-----------------------------3303153187906175792551542585\r\nContent-Disposition: form-data; name=\"name1\"\r\n\r\nvalue1\r\n-----------------------------3303153187906175792551542585\r\nContent-Disposition: form-data; name=\"name2\"\r\n\r\nvalue2\r\n-----------------------------3303153187906175792551542585\r\nContent-Disposition: form-data; name=\"name3\"\r\n\r\nvalue3\r\n-----------------------------3303153187906175792551542585--\r\n");

$response = curl_exec($ch);

这是请求标头:

Content-Length: 408
Content-Type: application/x-www-form-urlencoded
Accept: */*
Host: my_curl_test_site.com
X-Https: 1

这是请求正文:

-----------------------------3303153187906175792551542585
Content-Disposition: form-data; name="name1"

value1
-----------------------------3303153187906175792551542585
Content-Disposition: form-data; name="name2"

value2
-----------------------------3303153187906175792551542585
Content-Disposition: form-data; name="name3"

value3
-----------------------------3303153187906175792551542585

我base64编码一个pdf像这样:

$pdf = base64_encode(file_get_contents('example.pdf'));
    • 适用于您的PandaDoc API**

这是文档字段,请注意上面的$pdf。

------BoundaryXXXXXXXXX
Content-Disposition: form-data; name="file"; filename="Sample PandaDoc PDF with Field Tags.pdf"
Content-Type: application/pdf;

$pdf
------BoundaryXXXXXXXXX

您的内容类型可能需要为application/pdf;base64
他们的例子使用二进制数据。
这是您的数据字段

------BoundaryXXXXXXXXX
Content-Disposition: form-data; name="data"

{ 
   "name": "My minimal document",
   "url": "https://example.com/path/to/mydocument.pdf",
   "recipients": [
      {
       "email":"nobody@example.com"
      }
   ],
   "parse_form_fields": false
}
------BoundaryXXXXXXXXX

来源:https://developers.pandadoc.com/docs/upload-and-send-a-local-pdf

相关问题