Php – Pass array in post request using Guzzle

guzzleguzzle6php

I am trying to pass array in post request payload.

url = http://IP:PortNo/index.php/v1/login

payload:

data={ "terminal_id": "terminal_id", "api_login": "api_login", "api_key": "api_key", "merchant_code": "merchant_code" }

what i did is:

$data = ['terminal_id' => $this->terminal_id , 'api_login' => $this->api_login,
        'api_key' => $this->api_key , 'merchant_code' => $this->merchant_code];
$response = $this->client->post($url, array('data' => $data));

but the response i am getting is:

"Request data not found"

this means my data payload is not correctly formed. any idea?

Best Solution

$response = $this->client->post($url, ['json' => $data]);

See the Guzzle6 manual in the section entitled json: http://docs.guzzlephp.org/en/stable/request-options.html#json