【问题标题】:PHP GuzzleHttp . How to send a json post?PHP GuzzleHttp 。如何发送 json 帖子?
【发布时间】:2018-10-05 19:24:13
【问题描述】:

我有这个帖子:

{"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}']

但它不起作用,有什么方法可以发送所有内容而不像我发布的那样格式化它?谢谢!

【问题讨论】:

    标签: php symfony post guzzle goutte


    【解决方案1】:

    首先创建客户端对象

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

    然后是你的参数请求

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

    【讨论】:

    • 格式化它是有效的,但是为什么当我只是给那个网站给我的帖子时却不行? [ 'headers' => [ 'Content-Type' => 'application/json', 'Referer' => 'rituals.com/es-es/stores'], 'json' => ['{"latitude":"","longitude":" ","countryCode":"ES","filterPostalCode":"","filterCity":"","filterCountryCode":"ES","searchText":"","nextPageToken":0,"storeType":"正常","checkStoreAvailability":false}'], ]
    猜你喜欢
    • 2011-09-06
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多