P粉8484421852023-08-29 09:16:59
這是錯的:
$postfields['data'] = json_encode($data);
我發現他們的 API 文件令人不安。
簡短教學:
這是一個簡單的 multipart/form-data 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中發送此內容,您必須將表單資料放入postfields中。
$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
我對 pdf 進行 Base64 編碼,如下所示:
$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