【发布时间】:2026-02-01 15:15:02
【问题描述】:
我正在通过 Guzzle 调用 API。
public function request(string $method, string $uri, array $data = [], array $headers = [])
{
$response = $this->getClient()->$method($uri, [
'headers' => $headers,
'query' => $data,
]);
echo "1";
var_dump($response->getBody()->getContents());
$this->checkError($response);
echo "2";
var_dump($response->getBody()->getContents());
return $response;
}
public function checkError($response)
{
$json = json_decode($response->getBody()->getContents());
echo "3";
var_dump($json);
}
我的 json 测试(从“1”输出)是
{
"args":{
},
"headers":{
"Authorization":"Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
"Host":"httpbin.org",
"User-Agent":"GuzzleHttp/6.3.3 curl/7.59.0 PHP/7.2.4"
},
"origin":"1.2.3.4, 1.2.3.4",
"url":"https://httpbin.org/get"
}
但是,在代码“2”中,我有一个空字符串,而在代码“3”(“checkError”方法的输出)中,我有一个空字符串。
如果我注释掉 checkError 方法,我希望片段 2 中有另一个时间相同的 json,但我有一个空字符串。为什么会有这种行为?
【问题讨论】: