【问题标题】:Changing Header for Concurrent Requests (Guzzle)更改并发请求的标头(Guzzle)
【发布时间】:2017-09-17 20:27:51
【问题描述】:

发出异步请求时,我无法更改请求标头。

$requests = function ($total) {
    $uri = 'https://www.example.com';
    $headers = [
        'User-Agent' => 'testing/1.0',
        'Accept'     => 'application/json',
        'X-Foo'      => ['Bar', 'Baz']
    ];

    for ($i = 0; $i < $total; $i++) {
       yield new Request('GET', $uri, $headers); //Does not work
    }
};

$pool = new Pool($client, $requests(2), [
    'concurrency' => 5,
    'fulfilled' => function ($response, $index) {
        // this is delivered each successful response
    },
    'rejected' => function ($reason, $index) {
        // this is delivered each failed request
    },
]);

我相信我已经尝试了documentation 中提供的所有示例,并且能够更改除并发示例之外的所有示例的标题。任何帮助,将不胜感激。

【问题讨论】:

  • 我知道这篇文章到现在已经 2 岁了,但我发现自己有同样的问题
  • 抱歉,我不确定我是怎么做到的。我知道这个网站没有任何帮助。结帐 reddit。

标签: php concurrency guzzle guzzle6


【解决方案1】:

我可能已经让这个工作了,但不是在我希望的条件下。

$headers = ['User-Agent' => 'Bar'];
$client = new Client(['base_uri' => 'https://www.example.com/']);

$promises = [
    'image' => $client->getAsync('/page1',
        ['User-Agent' => "test"]
    ),
    'png'   => $client->getAsync('/page2'),
];

$results = Promise\unwrap($promises);

$results = Promise\settle($promises)->wait();

我相信这是可行的,因为在这种情况下,我使用的是 Client 对象,而不是创建自己的请求(就像我在另一个请求中所做的那样)。我对此并不完全确定。因此,如果有人可以提供任何进一步的见解,那将很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多