$baseUrl = 'http://foo';
$config = array();
$client = new Guzzle\Http\Client($baseUrl, $config);
Run Code Online (Sandbox Code Playgroud)
为Guzzle设置默认标头而不将其作为参数传递给每个人的新方法是什么$client->post($uri, $headers)?
有$client->setDefaultHeaders($headers),但它是不推荐使用.
setDefaultHeaders is deprecated. Use the request.options array to specify default request options
Run Code Online (Sandbox Code Playgroud)
tas*_*ski 32
如果您使用的是Guzzle v = 6.0.*
$client = new GuzzleHttp\Client(['headers' => ['X-Foo' => 'Bar']]);
Run Code Online (Sandbox Code Playgroud)
阅读文档,还有更多选择.
drj*_*201 26
$client = new Guzzle\Http\Client();
// Set a single header using path syntax
$client->setDefaultOption('headers/X-Foo', 'Bar');
// Set all headers
$client->setDefaultOption('headers', array('X-Foo' => 'Bar'));
Run Code Online (Sandbox Code Playgroud)
看这里:
http://docs.guzzlephp.org/en/latest/http-client/client.html#request-options
小智 5
正确,旧方法已被标记为@deprecated。这是为客户端上的多个请求设置默认标头的新建议方法。
// enter base url if needed
$url = "";
$headers = array('X-Foo' => 'Bar');
$client = new Guzzle\Http\Client($url, array(
"request.options" => array(
"headers" => $headers
)
));
Run Code Online (Sandbox Code Playgroud)