2017-04-21 151 views
0

我无法在发出异步请求时更改请求标头。更改并发请求标头(Guzzle)

$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提供的例子,并已能够改变标题为所有这些,除了并发实例。任何帮助,将不胜感激。

回答

0

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

$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(); 

我相信这工作,因为在这种情况下我使用的客户端对象,而不是创造我自己的要求(如我在其他人做)。我对此并不完全确定。所以如果任何人都可以提供进一步的见解,这将是有益的。