PHP,Curl和Dotloop-上传

时间:2018-06-21 21:41:57

标签: php curl

我正在使用PHP和Curl,并尝试通过其API将文档上传到Dotloop。我可以通过api访问Dotloop来下载文档,但上传令人困惑。也许您可以帮助我了解我在做什么错。

按照其API说明,他们声明要执行以下操作以通过其api上传文档:

上传文档

通过多部分表单发布上传单个文档(二进制)

(true, __lldb_expr_501.A<Swift.Int>(val: 4))
(true, __lldb_expr_501.A<Swift.String>(val: "hello"))
(false, __lldb_expr_501.A<Swift.Int>(val: 4))
B(_setOfA: Set([AnyHashable(__lldb_expr_501.A<Swift.String>(val: "hello")), AnyHashable(__lldb_expr_501.A<Swift.Int>(val: 4))]))
member = A<String>(val: "hello") : AnyHashable
   stringMember = A<String>(val: "hello") : A<String>
member = A<Int>(val: 4) : AnyHashable
   intMember = A<Int>(val: 4) : A<Int>

他们的示例代码:

        POST /profile/:profile_id/loop/:loop_id/folder/:folder_id/document/
        content-type: multipart/form-data; boundary=<BOUNDARY>
        content-length: XXX


        --<BOUNDARY>
        Content-Disposition: form-data; name="file"; filename="disclosures.pdf"
        Content-Type: application/pdf

        <binary data>
        --<BOUNDARY>--

我对他们的代码的PHP解释...

        $ curl -F 'file=@\"disclosures.pdf\";filename=\"disclosures.pdf\"" -H "Authorization: Bearer <token>" https://api-gateway.dotloop.com/public/v2/profile/:profile_id/loop/:loop_id/folder/:folder_id/document/           

更新

当我运行代码时,它停顿了大约2分钟,然后显示200 OK响应,但是文件不在我的Dotloop帐户中。

我知道我可以正常访问文件。当我回显文件的大小时,我得到以下信息:

        $localFile = "/Applications/XAMPP/xamppfiles/htdocs/file.pdf";
        $token = '************';
        $fp = fopen($localFile, 'rb');
        $size = filesize($localFile);
        $boundary = hash('sha256', uniqid('', true));
        $headers = [
            'Authorization: Bearer ' . $token,
            'content-type: multipart/form-data; boundary='.$boundary,
            'content-length: '.$size,
        ];
        $ch = curl_init('https://api-gateway.dotloop.com/public/v2/profile/*****/loop/*****/folder/*****/document/');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, [
            'file' => new \CURLFile($localFile, 'application/pdf','testFile123.pdf'),
        ]);
        $result = curl_exec($ch);
        $data = json_decode(trim($result), TRUE);
        print_r($data);

以下是Dotloops API文档以及上载文档的部分:https://dotloop.github.io/public-api/#upload-a-document

我做错了什么?

谢谢。

1 个答案:

答案 0 :(得分:1)

如果您尝试上传二进制数据,则您的curl请求绝对需要此请求:Can anyone give me an example for PHP's CURLFile class?目前,您所要做的只是向他们发送本地文件系统路径的字符串,这对他们。您使用curl_file_createnew CURLFile()

更改以下行

'file' => 'testFile123.pdf'

进入

'file' => new CURLFile($localFile,'application/pdf','testFile123.pdf')

编辑:删除content-typecontent-length headers,curl库应为您计算出这些值。保留Authorization标头。这意味着您可以删除fopen,因为CURLFile就是全部。