第一次錯誤:
[output] 【c.FormFile error==>】 multipart: NextPart: EOF
file 沒有傳值,或者非預定格式,stream resource
第二次錯誤:
[output] 【c.FormFile error==>】 multipart: NextPart: bufio: buffer full
header頭不需要 content-type => 'multipart/form-data'
php 調用參考:
你可以通過使用 multipart 請求參數來發送表單 (表單 enctype 屬性需要設置 multipart/form-data ) 文件, 該參數接收一個包含多個關聯數組的數組,每個關聯數組包含一下鍵名:
- name: (必須,字符串) 映射到表單字段的名稱。
- contents: (必須,混合) 提供一個字符串,可以是 fopen 返回的資源、或者一個
Psr\Http\Message\StreamInterface 的實例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
$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 格式如上,需要注意的是參數如果是數組,同樣需要重組,不重組就是第三個問題:
Invalid resource type: array
修改參數格式為 json, 畢竟跨語言 json xml 才是王道
1
2
3
4
5
6
7
8
9
10
|
``` $response = $client ->request( 'POST' , 'http://post' , [ 'multipart' => [ [ 'name' => 'field_name' , 'contents' => json_encode([ "abc" , "abd" ]) ] ] ] ]); |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://learnku.com/articles/37885