在php中透過CURL發送base64編碼的檔案和JSON
P粉139351297
P粉139351297 2023-08-28 10:22:44
0
1
638
<p>我正在嘗試使用 CURL 透過「從檔案 API 呼叫建立文件」將檔案傳送到 PANDADOCS:https://developers.pandadoc.com/reference/create-document-from-pdf。 </p> <p>除了發送文件之外,我還需要發送一個包含收件人等的資料對象,作為 multipart/form-data 字串的一部分以 JSON 形式發送。我不確定如何正確設置此調用,並且不斷收到從其 API 返回的各種錯誤訊息,例如“存在名為文件的字段”</p> <p>這是我到目前為止所擁有的:</p> <pre class="brush:php;toolbar:false;">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; }</pre> <p>誰能告訴我我做錯了什麼? </p>
P粉139351297
P粉139351297

全部回覆(1)
P粉848442185

這是錯的:

$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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板