当我使用Postman进行API调用时,我会收到一个JSON对象。这就是我所期望的。
然而当我像Guzzle那样打电话时:
$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.dev/']);
$response = $client->request('GET', 'search', [
'verify' => false,
]);
var_dump($response->getBody()); //null
var_dump($response); //returns below
我从Guzzle下面转储
Response {#304 ▼
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:8 [▼
"Server" => array:1 [▶]
"Content-Type" => array:1 [▼
0 => "application/json"
]
"Transfer-Encoding" => array:1 [▶]
"Connection" => array:1 [▶]
"Cache-Control" => array:1 [▶]
"Date" => array:1 [▶]
"X-RateLimit-Limit" => array:1 [▶]
"X-RateLimit-Remaining" => array:1 [▶]
]
-headerNames: array:8 [▼
"server" => "Server"
"content-type" => "Content-Type"
"transfer-encoding" => "Transfer-Encoding"
"connection" => "Connection"
"cache-control" => "Cache-Control"
"date" => "Date"
"x-ratelimit-limit" => "X-RateLimit-Limit"
"x-ratelimit-remaining" => "X-RateLimit-Remaining"
]
-protocol: "1.1"
-stream: Stream {#302 ▼
-stream: stream resource @15 ▼
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: []
}
}
答案 0 :(得分:16)
var width = $(window).width();
if(width < 768) {
$('.container-lg').addClass('container').removeClass('container-lg');
} else {
$('.container').addClass('container-lg').removeClass('container');
}
返回一个流。如果您想立即获取所有内容,可以使用getBody()
方法并在其中解码json(如果需要)
getContents()
进一步阅读 - Guzzle Responses
答案 1 :(得分:4)
如果$ response-> getBody()-> getContents()对您不起作用,请尝试:
$response->getBody()->__toString();
如here所述,有时getContents的流指针已经在流的末尾,然后返回空响应,但是__toString默认将其重置。