问题
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