php - curl 将文件发送到 api
问题描述
我无法通过 curl 向 api 发送文件。我有一个在邮递员中生成的代码,但不幸的是它不起作用。我从制片人那里得到了邮递员的图书馆。
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.accept.autenti.net/api/v0.1/documents/*********************/files",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('files'=> new CURLFILE('/path/to/file'),'files'=> new CURLFILE('/path/to/file')),
CURLOPT_HTTPHEADER => array(
"Authorization: *********************",
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
服务器响应:
{"error":{"id":"8fca0a50-8e8f-48e7-9855-30d27fdd45fd","key":"NO_FILES_GIVEN"}}
我根据文档对其进行了修改。我已添加到 CURLOPT_HTTPHEADER
"Content-Type: multipart / form-data; boundary = Content-Disposition: form-data; name = 2665; Content-Type: application / pdf",
"Content-Length: 192897"
目前它看起来像这样:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.accept.autenti.net/api/v0.1/documents/*********************/files",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('files'=> new CURLFILE('2665.pdf')),
CURLOPT_HTTPHEADER => array(
"Authorization: *********************",
"Content-Type: multipart/form-data; boundary=Content-Disposition: form-data; name=2665; Content-Type: application/pdf",
"Content-Length: 192897"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
服务器响应:
{"error":{"id":"c7a19220-f953-4ffd-893b-18914bbb161d","key":"FILE_SIZE_LIMIT_EXCEEDED"}}
这是文档的链接:https ://autenti.com/api/上传。
解决方案
那是邮递员的一个错误,生成的代码没有意义,如果你能被激怒,你应该向邮递员发送一个错误报告。
CURLOPT_POSTFIELDS => array(
'files' => new CURLFILE('/path/to/file'),
'files' => new CURLFILE('/path/to/file')
)
几乎不合法,而且是无意义的 php 代码,也许它应该是
CURLOPT_POSTFIELDS => array(
'files' => array(
0 => new CURLFILE('/path/to/file'),
1 => new CURLFILE('/path/to/file')
)
)
如果它确实是一个multipart/form-data
-post,那么这个标题就是错误的,很可能是生成器中的一个错误:"Content-Type: application/x-www-form-urlencoded"
并且不要"Content-Type: multipart / form-data; boundary
手动设置 -header,而是让 curl 自动为您生成它,也不要"Content-Length: 192897"
手动设置标题,这很可能是不正确的,因为长度取决于文件大小和边界的长度,甚至您正在上传的文件的文件名,如果您不这样做,curl 会自动为您生成标题(并且 curl 不会在长度标题中犯任何错误,与您不同)
这
{"error":{"id":"c7a19220-f953-4ffd-893b-18914bbb161d","key":"FILE_SIZE_LIMIT_EXCEEDED"}}
错误可能意味着您实际上超出了限制,但您的硬编码Content-Length: 192897
-header 更有可能具有虚假值,并且虚假的 content-length 标头值使服务器感到困惑并生成“FILE_SIZE_LIMIT_EXCEEDED”-错误。
但是浏览文档,在我看来应该是这样的:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL=> 'https://api.accept.autenti.net/api/v0.1/documents/*********************/files',
CURLOPT_HTTPAUTH => CURLAUTH_BEARER,
CURLOPT_XOAUTH2_BEARER=>"token",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS=>array(
new CURLFile($filename, "application/pdf")
)
));
curl_exec($ch);
...而且他们的示例代码很糟糕。这是上面代码生成的请求:
POST /api/v0.1/documents/*********************/files HTTP/1.1
Host: api.accept.autenti.net
Authorization: Bearer token
Accept: */*
Content-Length: 193
Content-Type: multipart/form-data; boundary=------------------------d058e8488f15fa10
--------------------------d058e8488f15fa10
Content-Disposition: form-data; name="0"; filename="test.file"
Content-Type: application/pdf
lol
--------------------------d058e8488f15fa10--
这看起来非常接近他们在示例代码中想要的......
推荐阅读
- c# - C# Ajax 获取请求
- php - Laravel Blade @foreach 指令未定义偏移量:1
- javascript - javascript 从 assoc 数组中删除(空)数组
- mysql - 如何将两个以上的表组合成一个具有自动增量 ID 的新表?
- shopify - Shopify - 液体:在分页页面上返回空 all_products 数组的集合
- c++ - 来自 jsonModel 的 QML 中的空 TreeView
- google-analytics - Adwords 未正确输入的 UTM 参数
- android - facebook 广告给 java.lang.NullPointerException
- c# - 将对象转换为具有指定区域性的字符串
- sapui5 - SAPUI5 mockserver.js 启动多个服务器