当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to set default header in Guzzle?

问题描述:

$baseUrl = 'http://foo';

$config = array();

$client = new Guzzle\Http\Client($baseUrl, $config);

What's the new way to set the default header for Guzzle without passing it as a parameter on every $client->post($uri, $headers)?

There's $client->setDefaultHeaders($headers) but it's deprecated.

setDefaultHeaders is deprecated. Use the request.options array to specify default request options

网友答案:
$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'));

See here:

http://docs.guzzlephp.org/en/latest/http-client/client.html#request-options

网友答案:

If you are using Guzzle v=6.0.*

$client = new GuzzleHttp\Client(['headers' => ['X-Foo' => 'Bar']]);

read the doc, there are more options.

网友答案:

Correct, the old method has been marked as @deprecated. Here is the new suggested method of setting default headers for multiple requests on the client.

// enter base url if needed
$url = ""; 
$headers = array('X-Foo' => 'Bar');

$client = new Guzzle\Http\Client($url, array(
    "request.options" => array(
       "headers" => $headers
    )
));
分享给朋友:
您可能感兴趣的文章:
随机阅读: