[ laravel爬虫实战--进阶篇 ] guzzle实现多张图片上传
Song •
1649 次浏览 •
0个评论 •
2023年01月14日
POST/表单请求
除了使用 body 参数来指定请求数据外,Guzzle为发送POST数据提供了有用的方法。
发送表单字段
发送 application/x-www-form-urlencoded POST请求需要你传入 form_params 数组参数,数组内指定POST的字段。
$response = $client->request('POST', 'http://httpbin.org/post', [
'form_params' => [
'field_name' => 'abc',
'other_field' => '123',
'nested_field' => [
'nested' => 'hello'
]
]
]);
发送表单文件
你可以通过使用 multipart 请求参数来发送表单(表单enctype属性需要设置 multipart/form-data )文件, 该参数接收一个包含多个关联数组的数组,每个关联数组包含一下键名:
- name: (必须,字符串) 映射到表单字段的名称。
- contents: (必须,混合) 提供一个字符串,可以是
fopen返回的资源、或者一个
Psr\Http\Message\StreamInterface 的实例。
$response = $client->request('POST', 'http://httpbin.org/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'
]
]
]
]);
演示案例
上传多图案例
$images = $request->file('images');
foreach ($images as $image)
{
$body[] = [
'Content-type' => 'multipart/form-data',
'name' => $image->getClientOriginalName(),
'contents' => fopen($image->getRealPath(), 'r')
];
}
$data = $request->all();
$client = new Client();
$response = $client->request('POST', $url, [
'multipart' => $body,
'json' => $data
]);
更多相关好文
-
laravel中distinct()的使用方法与去重 2017-09-11 -
Laravel将view缓存为静态html,laravel页面静态缓存 2021-10-09 -
[ laravel爬虫实战--基础篇 ] guzzle描述与安装 2017-11-01 -
[ 配置教程 ] 在ubuntu16.04中部署LNMP环境(php7+maridb且开启maridb远程以及nginx多域名访问 )并配置laravel环境 2017-07-18 -
微信公众号回复菜单点击回复文本 2025-10-24
热门文章
-
微信公众号回复菜单点击回复文本 2025-10-24 -
laravel+easywechat6出现No component_verify_ticket found以及修改缓存为redis 2025-09-09 -
nginx 服务器如何查看当前访问的域名 2025-06-10 -
ubuntu+nginx当服务器异常时微信/企业微信/抖音数据重复回调导致服务器崩溃怎么解决? 2025-05-30 -
Jquery使用xlsx实现批量导入Excel数据 2025-05-12

花生壳绑定ubuntu服务器?