gpt4 book ai didi

php - Guzzle 返回流空体而不是 json 体

转载 作者:可可西里 更新时间:2023-11-01 13:04:24 28 4
gpt4 key购买 nike

当我使用 Postman 进行 API 调用时,我收到一个 JSON 对象..这是我所期望的。 enter image description here

但是,当我像这样与 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: []
}
}

最佳答案

getBody() 返回一个流。如果您想一次获取所有内容,您可以使用 getContents() 方法并在其中解码 json(如果需要)

$payload = json_decode($response->getBody()->getContents());

进一步阅读 - Guzzle Responses

关于php - Guzzle 返回流空体而不是 json 体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44593272/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com