不能 POST Guzzle 6 HTTP 客户端中的布尔值

Can't POST boolean values in Guzzle 6 HTTP Client

我现在真的很沮丧,可以求助于对 Guzzle 更有经验的人。

我正在连接到一个 API,如果我想要来自 API 服务器的代码消息响应而不是完整的 link 响应,它需要一个布尔值。

以下是原始 http 客户端 (Postman) 访问的两种 return 类型之间差异的示例:

启用布尔代码:

禁用布尔代码:

我遇到的问题是,当我使用 Guzzle 6 发出相同的请求时,我总是会得到完整的 link 响应,并且永远无法在 post 正文中获得布尔值要施加。布尔参数似乎被字符串化为 'true'(这是我的猜测)。

因此以下两个 POST 请求产生完全相同的结果:

        // define request parameters
        $this->req_body = [
            'email' => $encrypted_email,
            'code'  => true
        ];

        // request url
        $request_url = $api_endpoint . self::RETURN_TYPE;

        // instance of Guzzle Client
        $client = $this
                    ->client();

        // abstract connection
        // XXX: this request needs no headers
        $response = $client
                        ->post($request_url, array(
                            'form_params' => $this->req_body
                        ));
        // real data
        $data = $response
                        ->getBody()
                        ->getContents();

        // send output
        $this->response = $data;

如果我尝试将 code 表单参数注释掉:

        // define request parameters
        $this->req_body = [
            'email' => $encrypted_email\,
            //'code'    => true
        ];

        // request url
        $request_url = $api_endpoint . self::RETURN_TYPE;

        // instance of Guzzle Client
        $client = $this
                    ->client();

        // abstract connection
        // XXX: this request needs no headers
        $response = $client
                        ->post($request_url, array(
                            'form_params' => $this->req_body
                        ));
        // real data
        $data = $response
                        ->getBody()
                        ->getContents();

        // send output
        $this->response = $data;

returned API 响应总是: "{"success":{"code":200,"message":"https:\/\/webservices.bvdpetroleum.com\/users\/user-password-reset\/q8VqSAbfTOkW0EMvSTfK5qSS4zr28rSwdQy3D\/uc9wtz3+RI4LH7hDkh\/ZbTfqcC"}}"

如何在 Guzzle 6 form_params 数组中发送 Boolean 值?任何见解将不胜感激,因为我不想切换到 CURL.

谢谢!

只需更改 form_params 数组以作为 json post 正文发送 :)

$response = $client->post($request_url, array(
               'json' => $this->req_body
            ));