第一次錯(cuò)誤:
[output] 【c.FormFile error==>】 multipart: NextPart: EOF
file 沒(méi)有傳值,或者非預(yù)定格式,stream resource
第二次錯(cuò)誤:
[output] 【c.FormFile error==>】 multipart: NextPart: bufio: buffer full
header頭不需要 content-type => 'multipart/form-data'
php 調(diào)用參考:
你可以通過(guò)使用 multipart 請(qǐng)求參數(shù)來(lái)發(fā)送表單 (表單 enctype 屬性需要設(shè)置 multipart/form-data ) 文件, 該參數(shù)接收一個(gè)包含多個(gè)關(guān)聯(lián)數(shù)組的數(shù)組,每個(gè)關(guān)聯(lián)數(shù)組包含一下鍵名:
- name: (必須,字符串) 映射到表單字段的名稱(chēng)。
- contents: (必須,混合) 提供一個(gè)字符串,可以是 fopen 返回的資源、或者一個(gè)
Psr\Http\Message\StreamInterface 的實(shí)例。
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 格式如上,需要注意的是參數(shù)如果是數(shù)組,同樣需要重組,不重組就是第三個(gè)問(wèn)題:
Invalid resource type: array
修改參數(shù)格式為 json, 畢竟跨語(yǔ)言 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" ]) ] ] ] ]); |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://learnku.com/articles/37885