Php guzzle 6的默认形式参数
是否有一种方法可以全局地将表单参数添加到guzzle 6的所有请求中 例如:Php guzzle 6的默认形式参数,php,guzzle,guzzle6,Php,Guzzle,Guzzle6,是否有一种方法可以全局地将表单参数添加到guzzle 6的所有请求中 例如: $client = new \GuzzleHttp\Client([ 'global_form_params' => [ // This isn't a real parameter 'XDEBUG_SESSION_START' => '11845', 'user_token' => '12345abc', ] ]); $client->pos
$client = new \GuzzleHttp\Client([
'global_form_params' => [ // This isn't a real parameter
'XDEBUG_SESSION_START' => '11845',
'user_token' => '12345abc',
]
]);
$client->post('/some/web/api', [
'form_params' => [
'some_parameter' => 'some value'
]
]);
在我的理想世界中,post
将产生数组合并global\u form\u params
和form\u params
:
[
'XDEBUG_SESSION_START' => '11845',
'user_token' => '12345abc',
'some_parameter' => 'some value',
]
我可以看到,对于query
或json
也需要类似的东西,根据您可以设置“任意数量的默认请求选项”和
将您的表单参数应用于每个请求
由于在中更改了内容类型
标题,这可能会导致GET请求出现问题。它最终将取决于服务器配置
如果您的意图是让客户端同时发出GET和POST请求,那么将form_参数移动到中间件中可能会更好。例如:
$stack->push(\GuzzleHttp\Middleware::mapRequest(function (RequestInterface $request) {
if ('POST' !== $request->getMethod()) {
// pass the request on through the middleware stack as-is
return $request;
}
// add the form-params to all post requests.
return new GuzzleHttp\Psr7\Request(
$request->getMethod(),
$request->getUri(),
$request->getHeaders() + ['Content-Type' => 'application/x-www-form-urlencoded'],
GuzzleHttp\Psr7\stream_for($request->getBody() . '&' . http_build_query($default_params_array)),
$request->getProtocolVersion()
);
});
我对此进行了尝试,但我希望将“默认”form_参数与每个请求中指定的内容合并。我认为我对“默认”一词的使用不清楚,我将更新我的问题。修改以反映所需的“合并”
$stack->push(\GuzzleHttp\Middleware::mapRequest(function (RequestInterface $request) {
if ('POST' !== $request->getMethod()) {
// pass the request on through the middleware stack as-is
return $request;
}
// add the form-params to all post requests.
return new GuzzleHttp\Psr7\Request(
$request->getMethod(),
$request->getUri(),
$request->getHeaders() + ['Content-Type' => 'application/x-www-form-urlencoded'],
GuzzleHttp\Psr7\stream_for($request->getBody() . '&' . http_build_query($default_params_array)),
$request->getProtocolVersion()
);
});