Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to post Guzzle 6 Async data

Tags:

php

guzzle

yii2

i trying to post data as Async with using Guzzle 6(latest ver)

    $client = new Client();
    $request = $client->postAsync($url, [
        'json' => [
                'company_name' => 'update Name'
        ],
    ]);

but i am not getting any request form Guzzle like post request on terminal

like image 496
RahulG Avatar asked Jul 02 '15 02:07

RahulG


People also ask

How do I send a POST request with guzzle?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

Is guzzle asynchronous?

Guzzle allows you to send both asynchronous and synchronous requests using the same interface and no direct dependency on an event loop. This flexibility allows Guzzle to send an HTTP request using the most appropriate HTTP handler based on the request being sent.

How do you send a header on guzzle?

// Set various headers on a request $client->request('GET', '/get', [ 'headers' => [ 'User-Agent' => 'testing/1.0', 'Accept' => 'application/json', 'X-Foo' => ['Bar', 'Baz'] ] ]); Headers may be added as default options when creating a client.

How do I log guzzle request?

Simple usage. use GuzzleLogMiddleware\LogMiddleware; use GuzzleHttp\HandlerStack; $logger = new Logger(); //A new PSR-3 Logger like Monolog $stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it.


1 Answers

Because it's a promise, you need to put then

and the promise will not called unless you put $promise->wait()

This is a simple post request using postAsync based on your question:

$client = new Client();
$promise = $client->postAsync($url, [
    'json' => [
            'company_name' => 'update Name'
    ],
])->then(
    function (ResponseInterface $res){
        $response = json_decode($res->getBody()->getContents());

        return $response;
    },
    function (RequestException $e) {
        $response = [];
        $response->data = $e->getMessage();

        return $response;
    }
);
$response = $promise->wait();
echo json_encode($response);
like image 71
faruk Avatar answered Oct 07 '22 06:10

faruk