Reputation: 85
I'm trying to send post async request
$client = new Client([
'timeout' => 2.0,
]);
$request = new \GuzzleHttp\Psr7\Request('POST', 'localhost/test.php' , [ 'json' => [
"username"=>"xyz",
"password"=>"xyz",
"first_name"=>"test",
"last_name"=>"test",
"email"=>"[email protected]",
"roles"=>"Administrator"
], ]);
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
test.php code is
var_dump($_POST);
The result should be the variables that i've set, but i get empty array.
Upvotes: 1
Views: 3107
Reputation: 1421
Use a scheduler function like below , read the register_shutdown_function
this function will be called at end of php script, so its a good time to close the connection and continue the heavy jobs.
public static function schedulePromise($callable, ...$args)
{
register_shutdown_function(function ($callable, ...$args) {
@session_write_close();
@ignore_user_abort(true);
call_user_func($callable, ...$args);
}, $callable, ...$args);
}
And the user never feels the heavy work behind the scenes. for example waiting for request in guzzle http
$promise = $client->sendAsync($request, ['timeout' => 10])
->then(function ($response) {
// success
}, function ($response) {
// failure
});
self::schedulePromise([$promise, 'wait'],false);
Upvotes: 0
Reputation: 4737
I believe the problem is that you are using "json" => []
, you might have to use "form_params" => []
to get things populated to the $_POST
array.
See some documentation here: http://docs.guzzlephp.org/en/stable/request-options.html#form-params
The json
option is used for RESTful APIs that explicitly require json as input, but in php normally $_POST
is populated with the posted form, for example the named <input>
s in a <form>
Upvotes: 3