Guzzle 6 POST 请求给出 401 未经授权的响应

Guzzle 6 POST Request Gives 401 Unauthorized Response

这是我用来同时发送多个 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;

但这给了我下面的错误信息。

Client error: POST http://10.11.1.5:8080/restnotification/application/com.*****.test/user/123456 resulted in a 401 Unauthorized response

但是,如果我使用正确的凭据通过邮递员发送相同的请求,它似乎可以正常工作。

请查看下面生成的 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
                (
                )

        )

)

我在这里做错了什么?请分享您的想法。谢谢。

很遗憾,我无法解决此错误。看来 promise 方法在我的情况下不起作用。

所以我所做的是在一个循环中迭代 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;