Reputation: 89
I want to send a asynchronous request using Guzzle PHP HTTP client, however it seems it only allows body to be a string .
I have header variable as
$headers = [
"Authorization" : $token
];
Similarly I want to have body also as array
$body = [
"x"=>$y,
"y"=>$z,
]
I make a request variable as
$request = new \GuzzleHttp\Psr7\Request(
'POST',
'API_URL',
$headers,
$body
);
However I get InvalidArgumentException Invalid resource type: array
error, but on trying $body="some useless string"
, the request is sent to the server, but get error as body doesn't have appropriate parameters .
How can I set Body here as an array/(nested array if required) with my desired keys.
Upvotes: 1
Views: 1158
Reputation: 89
Use json_encode
function, pass your body array by
$request = new \GuzzleHttp\Psr7\Request(
'POST',
'API_URL',
$headers,
json_encode($body)
);
Upvotes: 2