如何访问Guzzle超出其范围的完整响应? [英] How to access Guzzle's fullfilled response outside its scope?

查看:97
本文介绍了如何访问Guzzle超出其范围的完整响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是guzzle包的新手,并且正在使用pool-promise方法通过它发送异步发布请求.一切都很好,但是一旦请求得到满足并接收到响应,我将尝试将json响应的一部分存储在数组$arr中.

I am new to guzzle package and I am sending async post requests through it using pool-promise method. Everything is well and good but once the request is fulfilled and response is received, I am trying to store some part of json response in an array $arr.

$client = new Client();

        $arr = [];


        $requests = function ($total) use ($client) {

            $request_headers = [
                'api_key' => config('app.wallet_server_api_key'),
                'Content-Type' => 'application/x-www-form-urlencoded'
            ];


            $form_params = [
                'accounts' => 0,
                'totalaccounts' => 100,
            ];


            $uri = 'MY_REQUEST_URL_CAN_NOT_DISCLOSE';


            for ($i = 0; $i < $total; $i++) {
//                yield new Request('POST', $uri, $request_headers, http_build_query($form_params, null, '&'));

                yield function () use ($client, $uri, $request_headers, $form_params) {
                    return $client->postAsync($uri, [
                        'headers' => $request_headers,
                        'form_params' => $form_params
                    ]);
                };
            }


        };

$pool = new Pool($client, $requests(2), [
            'concurrency' => 5,
            'fulfilled' => function (Response $response, $index) use ($arr) {
                // Response is logged successfully
                Log::info(json_decode($response->getBody()->getContents(), true)['message']);

                // I am pushing the message key from json response received but it is not taking
                $arr[] = json_decode((string)$response->getBody()->getContents(), true)['message'];


            },
            'rejected' => function (RequestException $reason, $index) {
                // this is delivered each failed request
                Log::warning(json_encode($reason->getMessage()));
            },
        ]);

// Initiate the transfers and create a promise
        $promise = $pool->promise();

// Force the pool of requests to complete.
        $promise->wait();

dd($arr); // Displays as null

请帮助我了解其工作原理.

Please help me understand its working.

使用邮递员,我得到json格式的响应,如下所示:

using postman, I am getting response in json format like below:

{"status":true,"message":"Mnemonics fetched successfully",....SOME OTHER KEYS ETC }

推荐答案

在找到解决方案时回答我自己的问题.

Answering my own question as I found the solution.

如果您要设置匿名函数中的变量,请使用&$VariableName,就像我这样:

If you want to set variable in anonymous function, use &$VariableName as in my case it is:

'fulfilled' => function (Response $response, $index) use (&$arr) {  // <-- see the & sign here
                // Response is logged successfully
                Log::info(json_decode($response->getBody()->getContents(), true)['message']);

                // I am pushing the message key from json response received but it is not taking
                $arr[] = json_decode((string)$response->getBody()->getContents(), true)['message'];


            },

我希望像我这样的人会喜欢这个答案.

I hope someone like me will fond this answer useful.

这篇关于如何访问Guzzle超出其范围的完整响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆