[已解决] 如何读取 Guzzle 的 Response 响应结果中的 JSON 数据?
使用 [Guzzle](https://github.com/guzzle/guzzle)
时出现的一个问题:
$client = new \GuzzleHttp\Client();
# 获取一个外部 API 接口:
$response = $client->get('http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.87186,116.479723&output=json&pois=1&ak=Your_AK');
# echo 结果
echo $response->getBody();
输出结果:
renderReverse&&renderReverse({"status":0,"result":{"location":{"lng":116.47972299999994,"lat":39.87186008036971},"formatted_address":"北京市朝阳区西燕东街","business":"十八里店,南磨房,潘家园"c869","zip":""}}})
目前有个疑问,因为现在 Respone 好像使用了 PSR-7 stream objects,不太理解我现在要使用 json 数据的话,如要如何去读取?
直接
json_decode($response->getBody());
个人加了调用 callback,忘记处理了,所以一直无法解析 json 数据
高认可度评论:
json_decode($response->getBody()->getContents(), true)
json_decode($response->getBody()->getContents(), true)