先上代码:
use GuzzleHttp\Psr7; $response = $client->request('POST', 'http://httpbin.org/post', [ 'multipart' => [ [ 'name' => 'field_name', 'contents' => 'abc' ], [ 'name' => 'file_name', 'contents' => Psr7\Utils::tryFopen('/path/to/file', 'r') ], [ 'name' => 'other_file', 'contents' => 'hello', 'filename' => 'filename.txt', 'headers' => [ 'Content-Type' => 'multipart/form-data' ] ] ] ]);
说明:
name 当前form表单提交的字段
contents 对应当前from表单字段内容
该方法支持支持同时提交字符内容和文件
更多GuzzleHttp 请参考官方文档:https://docs.guzzlephp.org/en/stable/quickstart.html#post-form-requests