2017-04-21 6 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(); 

나는이 경우에 나는 클라이언트 객체를 사용하기보다는 (내가 다른 하나처럼) 내 자신의 요청을 생성하고 있기 때문에이 작품 생각합니다. 나는 이것에 대해 완전히 확신하지 못한다. 따라서 누군가가 더 이상의 통찰력을 제공 할 수 있다면 도움이 될 것입니다.