我正在使用一个名为 Slim 3 的 PHP 框架,我想检查我的Response对象,我想知道为什么这是不可能的。
官方文档指出 Response` 对象实现了 PSR 7 ResponseInterface 允许我检查正文。
当我var_dump($response->getBody();只看到一个protected身体并且$stack在任何地方都找不到我的内容时。
文档说明了这一点,我认为这就是原因。你能确认一下吗?
提醒 Response 对象是不可变的。此方法返回包含新正文的 Response 对象的副本。
来源:https : //www.slimframework.com/docs/objects/response.html
PHP 控制器类
class Controller
{
public function index($request, $response, $args)
{
// code
$stack = array($cars, $manufacturers);
$response = $response->withJson($stack);
return $response->withStatus(200);
}
}
Run Code Online (Sandbox Code Playgroud)
var_dump 的输出
class Slim\Http\Response#201 (5) {
protected $status =>
int(200)
protected $reasonPhrase =>
string(2) "OK"
protected $protocolVersion =>
string(3) "1.1"
protected $headers =>
class …Run Code Online (Sandbox Code Playgroud) 我无法访问我的 json,想知道错误在哪里。
$json = json_encode($data);
// string(1065) "[{"id":"test123","key":["one",...
Run Code Online (Sandbox Code Playgroud)
然后我对其进行解码以使其可以通过以下命令访问:
$json = json_decode($json, true);
// Output:
array(3) {
[0]=>
array(4) {
["id"]=>
string(14) "test123"
Run Code Online (Sandbox Code Playgroud)
当我想回应它时,它给了我:
echo $json[0];
Run Code Online (Sandbox Code Playgroud)
数组到字符串的转换