Using PHP Guzzle HTTP 6 to send JSON with already encoded data

I'm trying to send a POST request that contains a JSON string with the following headline: Content-Type: application/json.

From looking at the documents, I see that I can do something like this ...

$data = ['x' => 1, 'y' => 2, 'z' => 3];
$client = new \GuzzleHttp\Client($guzzleConfig);
$options = [
    'json' => $data,
];
$client->post('http://example.com', $options);

My problem is that when I get to this point, $datathere was already json_encode'd.

I tried the following, but it does not work.

$data = json_encode(['x' => 1, 'y' => 2, 'z' => 3]);
$client = new \GuzzleHttp\Client($guzzleConfig);
$options = [
    'body' => $data,
    'headers' => ['Content-Type' => 'application/json'],
];
$client->post('http://example.com', $options);

My question is: can I use a parameter jsonwith an already encoded array? Or is there a way for me to just set the title Content-Type?

+4
source share
2 answers

guzzle docs http://docs.guzzlephp.org/en/latest/request-options.html#json

json body

. Content-Type PHP json_encode(). , JSON , , Content-Type, .

, form_params multipart

+2

? Guzzle json , Content-Type. . PUT.

$response = $client->request('PUT', '/put', ['json' => ['foo' => 'bar']]);
-1

All Articles