我正在使用PHP和Curl,并试图通过他们的API将文档上传到Dotloop。我可以通过api访问Dotloop来下载文档,但是上传很混乱。也许你能帮我弄明白我做错了什么。
根据他们的API说明,他们声明要执行以下操作以通过其api上传文档:
上传文档的
通过多部分表单发布上传单个文档(二进制)
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>--
他们的示例代码:
$ 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/
我的PHP解释他们的代码...
$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);
更新
当我运行我的代码时,它会延迟大约2分钟,然后它会显示200OK响应,但是这个文件不在我的Dotloop帐户中。
我知道我可以很好地访问文件。当我回显文件的大小时,我得到的结果是:
echo $size; // the size is 116625
下面是Dotloops API文档&上传文档的部分:https://dotloop.github.io/public-api/#upload-a-document
我做错了什么?
谢谢。
发布于 2018-06-22 06:02:05
如果您正在尝试上传二进制数据,那么您的curl请求绝对需要这个:Can anyone give me an example for PHP's CURLFile class?目前您所做的只是向它们发送一个包含本地文件系统路径的字符串,而这对它们没有任何用处。您可以使用curl_file_create或new CURLFile()
更改以下行
'file' => 'testFile123.pdf'
转到
'file' => new CURLFile($localFile,'application/pdf','testFile123.pdf')
编辑:删除content-type
和content-length headers
,curl库应该会为您计算这些内容。保留Authorization
标头。这意味着您可以删除fopen
内容,因为只需要CURLFile
。
https://stackoverflow.com/questions/50977804
复制相似问题