簡體   English   中英

curl 將文件發送到 api

[英]curl send the file to 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 具有虛假值,並且虛假的內容長度標頭值使服務器感到困惑並生成“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--

這看起來非常接近他們在示例代碼中想要的......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM