PHP GuzzleHttp。如何发送json帖子?

时间:2018-04-25 10:41:45

标签: php symfony post guzzle goutte

我有这篇文章:

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

我试图像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}']

但它没有工作,任何方式发送所有内容而不像我发布的那样形成它?谢谢!

1 个答案:

答案 0 :(得分:1)

首先创建客户端对象

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

然后你的请求用params

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