【问题标题】:How to set default header in Guzzle?如何在 Guzzle 中设置默认标题?
【发布时间】:2013-07-19 22:50:00
【问题描述】:
$baseUrl = 'http://foo';
$config = array();
$client = new Guzzle\Http\Client($baseUrl, $config);

为 Guzzle 设置默认标头而不将其作为参数传递给每个 $client->post($uri, $headers) 的新方法是什么?

$client->setDefaultHeaders($headers),但它已被弃用。

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

【问题讨论】:

    标签: php guzzle


    【解决方案1】:

    如果您使用的是 Guzzle v=6.0.*

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

    read the doc,还有更多选择。

    【讨论】:

    • 根据文档标题是请求选项,而不是客户端选项。两者都可以互换吗?
    • 您在客户端实例中设置它,但使用键 'headers' => [...] (如上所示)。没有测试它,但假设可以改变......
    • 有谁知道如何在客户端实例化后添加默认标头?
    【解决方案2】:
    $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'));
    

    看这里:

    http://docs.guzzlephp.org/en/5.3/clients.html#request-options

    【讨论】:

    • 如何对基本身份验证用户名执行相同操作并通过?
    • 在 Guzzle 6 中,您只能在客户端实例化时设置默认选项。如果您必须使用现有实例,则无法再对其进行配置。见What replaces client->setDefaultOption?。 “哦,嘿,让我们让事情变得不那么灵活,只是因为。看起来很多Enterprise”。叹息。
    • 当您想要进行单元测试并且想要将 Client 注入您的课程时,这是如何工作的?
    【解决方案3】:

    正确,旧方法已被标记为@deprecated。这是在客户端上为多个请求设置默认标头的新建议方法。

    // enter base url if needed
    $url = ""; 
    $headers = array('X-Foo' => 'Bar');
    
    $client = new Guzzle\Http\Client($url, array(
        "request.options" => array(
           "headers" => $headers
        )
    ));
    

    【讨论】:

      【解决方案4】:

      如果您使用 drupal,这对我有用;

      <?php
      $url = 'https://jsonplaceholder.typicode.com/posts';
      
      $client = \Drupal::httpClient();
      
      $post_data = $form_state->cleanValues()->getValues();
      
      $response = $client->request('POST', $url, [
          'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
          'form_params' => $post_data,
          'verify' => false,
      ]);
      
      $body = $response->getBody()->getContents();
      $status = $response->getStatusCode();
      
      dsm($body);
      dsm($status);
      

      【讨论】:

        【解决方案5】:

        要为 Guzzle 客户端设置默认标头(如果将客户端用作多个请求的基础),最好设置一个中间件,该中间件会在每个请求中添加标头。否则,其他请求标头将在新的客户端请求中被覆盖。

        例如:

        use GuzzleHttp\Client;
        use GuzzleHttp\HandlerStack;
        use GuzzleHttp\Middleware;
        use Psr\Http\Message\RequestInterface;
        
        ...
        
        $handler = HandlerStack::create();
        $handler->push(Middleware::mapRequest(function (RequestInterface $request) {
            return $request->withHeader('Authorization', "Bearer {$someAccessToken}");
        }));
        
        $client = new Client([
            'base_uri' => 'https://example.com',
            'handler' => $handler,
        ]);
        

        Middlware Docs

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-07
          • 2016-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多