Guzzle 6 result returns with HTTP Headers

[亡魂溺海] 提交于 2019-12-13 14:17:10

问题


I'm having a problem with how Guzzle 6.x returns my results, due to this I am receiving "Syntax Error" when I try to json_decode my results. I have used var_dump() and print_r() to get my actual results and it turns out that my JSON results returns with a HTTP Header included in it. Actual results below:

string(486) "Array
(
    [Content-Type] => application/json
    [Content-Length] => 23
    [X-Auth-Key] => XXXXXXXXXXXXXXXXXXXXXXX
    [X-Apikey] => XXXXXXXXX
    [User-Agent] => GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.23
    [Host] => example.com
)
{
    "message": {
        "value": "successful",
        "status_code": "200"
    },
    "details": {
        "status": [
            "00:00|Data 1|"
        ]
    }
}"

I've done my research and I've tried altering my code I've tried the following:

$result = (string) $response->getBody(); // 1

$result = $response->getBody()->getContents(); // 2

Below is my code on how I send HTTP requests to my server:

$response = $this->client->post('', [RequestOptions::JSON => $data]);
$result = $response->getBody()->getContents();
$contents = json_decode($result); // this returns NULL and syntax error

My expected results should be this:

{
    "message": {
        "value": "successful",
        "status_code": "200"
    },
    "details": {
        "status": [
            "00:00|Data 1|"
        ]
    }
}

Hope you'll be able to help me.

来源:https://stackoverflow.com/questions/56004396/guzzle-6-result-returns-with-http-headers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!