首页  >  问答  >  正文

在php中通过CURL发送base64编码的文件和JSON

<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粉139351297P粉139351297441 天前571

全部回复(1)我来回复

  • P粉848442185

    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

    回复
    0
  • 取消回复