我的工作Guzzle5代码大致如下:
$guzzle = new \GuzzleHttp\Client();
$request = $guzzle->createRequest('POST', $url);
$request->setHeader('Authorization', 'Bearer ' . $token);
$postBody = $request->getBody();
$postBody->setField('name', 'content');//several times
if (check for file) {
$postBody->addFile(new \GuzzleHttp\Post\PostFile('name', fopen(...));
}
$response = $guzzle->send($request);如果设置一个头文件并可能添加一个文件,我不知道如何使用Guzzle6来完成这个任务。
发布于 2015-06-25 17:34:12
这里是官方文档中的一个示例,您如何使用Guzzle 6设置标题并将文件添加到POST请求中:
$client = new \GuzzleHttp\Client();
$client->post('/post', [
'multipart' => [
[
'name' => 'foo',
'contents' => 'data',
'headers' => ['X-Baz' => 'bar']
],
[
'name' => 'baz',
'contents' => fopen('/path/to/file', 'r')
],
[
'name' => 'qux',
'contents' => fopen('/path/to/file', 'r'),
'filename' => 'custom_filename.txt'
],
]
]);多部分选项将请求的主体设置为多部分/表单数据表单,如果不需要处理文件,则只需使用参数而不是多部分选项即可。
任何可以使用帮助标头选项轻松设置的标头。
更多信息,您可以在这里找到口吻升级指南(5.0至6.0)
发布于 2015-06-24 23:38:39
下面是从我的一个项目中复制的一些代码:
$client = new GuzzleHttp\Client();
$url = 'someurl.com/api';
$body = json_encode([
'variable1' => 'this',
'variable2' => 'that'
]);
$response = $client->post($url, [
'headers' => [
'header_variable1' => 'foo',
'header_variable2' => 'bar'
],
'json' => true,
'timeout' => 3,
'body' => $body
]);
$data = $response->json();https://stackoverflow.com/questions/31038444
复制相似问题