Send base64 encoded file and JSON via CURL in php
P粉139351297
P粉139351297 2023-08-28 10:22:44
0
1
416

I'm trying to use CURL to send a file to PANDADOCS via the Create Document from File API call: https://developers.pandadoc.com/reference/create-document-from-pdf.

In addition to sending the file, I also need to send a data object containing the recipients etc. as part of a multipart/form-data string in JSON. I'm not sure how to set up this call correctly and I keep getting various error messages returned from its API, such as "A field named file exists"

This is what I have so far:

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; }

Can anyone tell me what I'm doing wrong?

P粉139351297
P粉139351297

reply all (1)
P粉848442185

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.

To send this in curl you have to put the form data into postfields.

$ch = curl_init(); curl_setopt($ch, CURLOPT_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 beapplication/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

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!