我正在尝试使用 CURL 通过“从文件 API 调用创建文档”将文件发送到 PANDADOCS:https://developers.pandadoc.com/reference/create-document-from-pdf。
除了发送文件之外,我还需要发送一个包含收件人等的数据对象,作为 multipart/form-data 字符串的一部分以 JSON 形式发送。我不确定如何正确设置此调用,并且不断收到从其 API 返回的各种错误消息,例如“存在名为文件的字段”
这是我到目前为止所拥有的:
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;
}
谁能告诉我我做错了什么?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号