这是我用来同时发送多个guzzle请求的代码。
<?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;
但是这给了我以下错误信息。
客户端错误:
POST http://10.11.1.5:8080/restnotification/application/com.*****.test/user/123456
导致401 Unauthorized
响应
但是,如果我通过邮递员以正确的凭据发送相同的请求,它似乎正在运作。
请参阅下面生成的guzzle请求数据。
GuzzleHttp\Psr7\Request Object
(
[method:GuzzleHttp\Psr7\Request:private] => POST
[requestTarget:GuzzleHttp\Psr7\Request:private] =>
[uri:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Uri Object
(
[scheme:GuzzleHttp\Psr7\Uri:private] => http
[userInfo:GuzzleHttp\Psr7\Uri:private] =>
[host:GuzzleHttp\Psr7\Uri:private] => 10.11.1.5
[port:GuzzleHttp\Psr7\Uri:private] => 8080
[path:GuzzleHttp\Psr7\Uri:private] => /restnotification/application/com.*****.test/user/dileep
[query:GuzzleHttp\Psr7\Uri:private] =>
[fragment:GuzzleHttp\Psr7\Uri:private] =>
)
[headers:GuzzleHttp\Psr7\Request:private] => Array
(
[User-Agent] => Array
(
[0] => GuzzleHttp/6.2.1 curl/7.50.1 PHP/7.0.10
)
[Host] => Array
(
[0] => 10.11.1.5:8080
)
[cookies] => Array
(
[0] => 1
)
[auth] => Array
(
[0] => ******
[1] => ******
)
[json] => Array
(
[alert] => Test Title
[data] => test details testing
)
[debug] => Array
(
[0] => 1
)
[headers] => Array
(
[Content-Type] => application/json
[Cache-Control] => no-cache
[X-SMP-DATA] => testng
)
)
[headerNames:GuzzleHttp\Psr7\Request:private] => Array
(
[user-agent] => User-Agent
[host] => Host
[cookies] => cookies
[auth] => auth
[json] => json
[debug] => debug
[headers] => headers
)
[protocol:GuzzleHttp\Psr7\Request:private] => 1.1
[stream:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Stream Object
(
[stream:GuzzleHttp\Psr7\Stream:private] => Resource id #378
[size:GuzzleHttp\Psr7\Stream:private] => 0
[seekable:GuzzleHttp\Psr7\Stream:private] => 1
[readable:GuzzleHttp\Psr7\Stream:private] => 1
[writable:GuzzleHttp\Psr7\Stream:private] => 1
[uri:GuzzleHttp\Psr7\Stream:private] => php://temp
[customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
(
)
)
)
我在这里做错了什么?请分享你的想法。谢谢。
答案 0 :(得分:0)
遗憾的是,我无法解决此错误。似乎承诺方法在我的案例中不起作用。
所以我做的是在循环中迭代guzzle post。在我的情况下,Guzzle帖子没有任何问题。
在我的案例中有效的示例代码如下:
<?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;