Laravel + GuzzleHTTP 没有响应数据?
Laravel + GuzzleHTTP no response data?
我正在使用 guzzle 查询我的 API。
$client = new Client();
$response = $client->post('xxxxxx',
array(
'headers' => array('Content-Type'=>'application/json'),
'json'=> array(
"type" => 0,
[...]
)
)
);
//$response = json_decode($response);
dd($response->getBody());
这应该输出如下内容:
{
"returnCode": 0,
"success": true
}
但我得到了其他东西。
Stream {#230 ▼
-stream: stream resource @12 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
任何人都可以帮助我或告诉我我做错了什么吗?我想发送原始 post 数据并在之后获取原始 post 数据然后我想保存(例如在数据库中)。
getBody()
returns 一个流接口。您想获取该流的内容,因此:
$response->getBody()->getContents();
是您要搜索的内容
备注
根据文档,您还可以将流接口转换为字符串,但我得到了不同的结果。 getContents()
总是做它需要做的,所以这是我的解决方案。
我正在使用 guzzle 查询我的 API。
$client = new Client();
$response = $client->post('xxxxxx',
array(
'headers' => array('Content-Type'=>'application/json'),
'json'=> array(
"type" => 0,
[...]
)
)
);
//$response = json_decode($response);
dd($response->getBody());
这应该输出如下内容:
{
"returnCode": 0,
"success": true
}
但我得到了其他东西。
Stream {#230 ▼
-stream: stream resource @12 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
任何人都可以帮助我或告诉我我做错了什么吗?我想发送原始 post 数据并在之后获取原始 post 数据然后我想保存(例如在数据库中)。
getBody()
returns 一个流接口。您想获取该流的内容,因此:
$response->getBody()->getContents();
是您要搜索的内容
备注
根据文档,您还可以将流接口转换为字符串,但我得到了不同的结果。 getContents()
总是做它需要做的,所以这是我的解决方案。