Search code examples
phpsymfonypostguzzlegoutte

PHP GuzzleHttp . How to send a json post?


I have this post:

{"latitude":"","longitude":"","countryCode":"ES","filterPostalCode":"","filterCity":"","filterCountryCode":"ES","searchText":"","nextPageToken":0,"storeType":"normal","checkStoreAvailability":false}

And i'm trying to send it like this on Guzzle 6.0+

'headers' => [
                'Content-Type' => 'application/json',
                'Referer' => 'https://www.rituals.com/es-es/stores'],
'body' => '{"latitude":"","longitude":"","countryCode":"ES","filterPostalCode":"","filterCity":"","filterCountryCode":"ES","searchText":"","nextPageToken":0,"storeType":"normal","checkStoreAvailability":false}']

But it's not working, any way to send everything without formating it like I posted? thanks!


Solution

  • First of create Client object

    $client = new Client([
         'http_errors' => false,
         'verify'      => false,
    ]);
    

    And then your request with params

    $response = $client->request($requestMethod, $url, array_merge(
        ['json' => $body],
        ['headers' => $headers]
    ));