简体   繁体   English

curl 将文件发送到 api

[英]curl send the file to api

I'm having trouble sending a file to api via curl.我无法通过 curl 将文件发送到 api。 I have a code that was generated in postman but unfortunately it doesn't work.我有一个在邮递员中生成的代码,但不幸的是它不起作用。 I got the libraries for postman from the producer.我从制作人那里得到了邮递员的图书馆。

$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;

server response:服务器响应:

{"error":{"id":"8fca0a50-8e8f-48e7-9855-30d27fdd45fd","key":"NO_FILES_GIVEN"}}

I modified it according to the documentation.我根据文档修改了它。 I have added to CURLOPT_HTTPHEADER我已经添加到 CURLOPT_HTTPHEADER

"Content-Type: multipart / form-data; boundary = Content-Disposition: form-data; name = 2665; Content-Type: application / pdf",
"Content-Length: 192897"

And at the moment it looks like this:目前它看起来像这样:

$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;

server response:服务器响应:

{"error":{"id":"c7a19220-f953-4ffd-893b-18914bbb161d","key":"FILE_SIZE_LIMIT_EXCEEDED"}}

Here is the link to the documentation : https://autenti.com/api/ Upload.这是文档的链接: https : //autenti.com/api/上传。

that's a bug in postman, the generated code makes no sense, you should send a bugreport to postman, if you can be arsed.这是邮递员中的一个错误,生成的代码没有意义,如果您可以被激怒,您应该向邮递员发送错误报告。

    CURLOPT_POSTFIELDS => array(
        'files' => new CURLFILE('/path/to/file'),
        'files' => new CURLFILE('/path/to/file')
    )

is barely legal, and nonsensical php code, perhaps it was supposed to be几乎不合法,而且毫无意义的 php 代码,也许它应该是

    CURLOPT_POSTFIELDS => array(
        'files' => array(
            0 => new CURLFILE('/path/to/file'),
            1 => new CURLFILE('/path/to/file')
        )
    )

also if it's indeed a multipart/form-data -post, this header is just wrong , a bug in the generator most likely: "Content-Type: application/x-www-form-urlencoded"此外,如果它确实是multipart/form-data -post,则此标题是错误的,很可能是生成器中的错误: "Content-Type: application/x-www-form-urlencoded"

and do not set the "Content-Type: multipart / form-data; boundary -header manually, instead let curl generate it for you automatically, also do not set the "Content-Length: 192897" header manually, it's VERY likely to be incorrect, as the length depends on the file size AND the length of the boundary AND even the filename of the file you're uploading, and curl will generate the header for you automatically if you don't (and curl won't make any mistakes in the length headers, unlike you)并且不要手动设置"Content-Type: multipart / form-data; boundary -header,而是让 curl 自动为你生成它,也不要手动设置"Content-Length: 192897"标题,它可能是不正确,因为长度取决于文件大小和边界的长度,甚至是您上传的文件的文件名,如果您不这样做,curl 会自动为您生成标题(并且 curl 不会生成任何长度标题中的错误,与您不同)

the

{"error":{"id":"c7a19220-f953-4ffd-893b-18914bbb161d","key":"FILE_SIZE_LIMIT_EXCEEDED"}}

error could mean that you actually exceeded the limit, but it's more likely that your hardcoded Content-Length: 192897 -header has a bogus value, and the bogus content-length header value made the server confused and generate the "FILE_SIZE_LIMIT_EXCEEDED"-error.错误可能意味着您实际上超出了限制,但更有可能是您的硬编码Content-Length: 192897 -header 具有虚假值,并且虚假的内容长度标头值使服务器感到困惑并生成“FILE_SIZE_LIMIT_EXCEEDED”-错误。

but skimming through the documentation, looks to me like it should be:但是浏览文档,在我看来它应该是:

$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);

... and that their example code is terrible. ...而且他们的示例代码很糟糕。 here is the request generated by the above code:这是上面代码生成的请求:

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--

which looks very close to what they want in the sample code...这看起来非常接近他们在示例代码中想要的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM