Laravel Guzzle 6 POST请求给出401个未经授权的响应
这是我用来同时发送多个guzzle请求的代码Laravel Guzzle 6 POST请求给出401个未经授权的响应,laravel,guzzle,guzzle6,Laravel,Guzzle,Guzzle6,这是我用来同时发送多个guzzle请求的代码 <?php //initialize test values. $results = []; $smpUrls = []; $client = new Client(); $authHeader = [ 'cookies' => true, 'auth' => [$this->username, $this->password],
<?php
//initialize test values.
$results = [];
$smpUrls = [];
$client = new Client();
$authHeader = [
'cookies' => true,
'auth' => [$this->username, $this->password],
'json' => [
"alert" => 'test title',
"data" => 'test data sample'
],
'debug' => true,
'headers' => [
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache',
'X-SMP-DATA' => 'testng'
],
];
$technicianIds = ["123456"];
//preparing urls
foreach ($technicianIds as $technicianId) {
$smpUrl = 'http://10.11.1.5:8080/restnotification/application/com.****.test/user/' . $technicianId;
$smpUrls[$technicianId] = $smpUrl;
}
//generate function to deal with multiple requests.
$requests = function ($urls, $headers) {
foreach ($urls as $url) {
yield new Request('POST', $url, $headers);
}
};
/**
* initialize guzzle pool in order to deal with individual request
* response or exception
*/
$pool = new Pool($client, $requests($smpUrls, $authHeader), [
//process only four requests cuncurently.
'concurrency' => 4,
//deal with succesful request response.
'fulfilled' => function ($response, $index) use (&$results) {
//json decode output, collect name and append to result.
\Log::info('fullfilled-response');
\Log::info($response);
},
//deal with exception of individual request.
'rejected' => function ($reason, $index) use (&$results) {
\Log::info('rejected-reason');
\Log::info($reason);
}
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
return $results;
我做错了什么?请分享你的想法。谢谢。很遗憾,我无法解决此错误。在我的情况下,承诺方法似乎不起作用 所以我所做的就是在一个循环中迭代guzzle post。Guzzle post在我的情况下没有任何问题 在我的案例中起作用的示例代码如下所示:
<?php
//initialize test values.
$results = [];
$smpUrls = [];
$client = new Client();
$authHeader = [
'cookies' => true,
'auth' => [$this->username, $this->password],
'json' => [
"alert" => 'test title',
"data" => 'test data sample'
],
'debug' => true,
'headers' => [
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache',
'X-SMP-DATA' => 'testng'
],
];
$technicianIds = ["123456"];
//preparing urls
foreach ($technicianIds as $technicianId) {
$smpUrl = 'http://10.11.1.5:8080/restnotification/application/com.****.test/user/' . $technicianId;
$results[] = $client->post($smpUrl, $authHeader);
}
return $results;
<?php
//initialize test values.
$results = [];
$smpUrls = [];
$client = new Client();
$authHeader = [
'cookies' => true,
'auth' => [$this->username, $this->password],
'json' => [
"alert" => 'test title',
"data" => 'test data sample'
],
'debug' => true,
'headers' => [
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache',
'X-SMP-DATA' => 'testng'
],
];
$technicianIds = ["123456"];
//preparing urls
foreach ($technicianIds as $technicianId) {
$smpUrl = 'http://10.11.1.5:8080/restnotification/application/com.****.test/user/' . $technicianId;
$results[] = $client->post($smpUrl, $authHeader);
}
return $results;