P粉8484421852023-08-29 09:16:59
This is wrong:
$postfields['data'] = json_encode($data);
I find their API documentation disturbing.
Short Tutorial:
This is a simple multipart/form-data HTML form:
Below I translate this form into 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>
To send this in curl you have to put the form data into 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);
This is the request header:
Content-Length: 408 Content-Type: application/x-www-form-urlencoded Accept: */* Host: my_curl_test_site.com X-Https: 1
This is the request body:
-----------------------------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
I Base64 encode the pdf like this:
$pdf = base64_encode(file_get_contents('example.pdf'));
For your PandaDoc API
This is the document field, please note $pdf above.
------BoundaryXXXXXXXXX Content-Disposition: form-data; name="file"; filename="Sample PandaDoc PDF with Field Tags.pdf" Content-Type: application/pdf; $pdf ------BoundaryXXXXXXXXX
Your content type may need to be application/pdf;base64
Their examples use binary data.
This is your data field
------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
Source: https://developers.pandadoc.com /docs/upload-and-send-a-local-pdf