雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

PHP編程

當(dāng)前位置:主頁 > 網(wǎng)絡(luò)編程 > PHP編程 >

記Laravel調(diào)用Gin接口調(diào)用formData上傳文件的實現(xiàn)方法

來源:本站原創(chuàng)|時間:2020-01-11|欄目:PHP編程|點擊:

第一次錯誤:

[output] 【c.FormFile error==>】 multipart: NextPart: EOF

file 沒有傳值,或者非預(yù)定格式,stream resource

第二次錯誤:

[output] 【c.FormFile error==>】 multipart: NextPart: bufio: buffer full

header頭不需要 content-type => 'multipart/form-data'

php 調(diào)用參考:

你可以通過使用 multipart 請求參數(shù)來發(fā)送表單 (表單 enctype 屬性需要設(shè)置 multipart/form-data ) 文件, 該參數(shù)接收一個包含多個關(guān)聯(lián)數(shù)組的數(shù)組,每個關(guān)聯(lián)數(shù)組包含一下鍵名:

  • name: (必須,字符串) 映射到表單字段的名稱。
  • contents: (必須,混合) 提供一個字符串,可以是 fopen 返回的資源、或者一個

Psr\Http\Message\StreamInterface 的實例。

$response = $client->request('POST', 'http://post', [
  'multipart' => [
    [
      'name'   => 'field_name',
      'contents' => 'abc'
    ],
    [
      'name'   => 'file_name',
      'contents' => fopen('/path/to/file', 'r')
    ],
    [
      'name'   => 'other_file',
      'contents' => 'hello',
      'filename' => 'filename.txt',
      'headers' => [
        'X-Foo' => 'this is an extra header to include'
      ]
    ]
  ]
]);

ps:
multipart 格式如上,需要注意的是參數(shù)如果是數(shù)組,同樣需要重組,不重組就是第三個問題:
Invalid resource type: array

修改參數(shù)格式為 json, 畢竟跨語言 json xml 才是王道

```
$response = $client->request('POST', 'http://post', [
    'multipart' => [
      [
        'name'   => 'field_name',
        'contents' => json_encode(["abc", "abd"])
      ]
    ]
  ]
]);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:PHP設(shè)計模式之裝飾器(裝飾者)模式(Decorator)入門與應(yīng)用詳解

欄    目:PHP編程

下一篇:PHP設(shè)計模式之外觀模式(Facade)入門與應(yīng)用詳解

本文標題:記Laravel調(diào)用Gin接口調(diào)用formData上傳文件的實現(xiàn)方法

本文地址:http://www.jygsgssxh.com/a1/PHPbiancheng/11027.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有