如何从Zend Framework 3的响应中获取有效的JSON输出? [英] How get a valid JSON output from the response in Zend Framework 3?
本文介绍了如何从Zend Framework 3的响应中获取有效的JSON输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为API编写客户端...
I'm writing a client for an API...
use Zend\Http\Client;
use Zend\Http\Request;
use Zend\Json\Json;
...
$request = new Request();
$request->getHeaders()->addHeaders([
'Accept-Charset' => 'UTF-8',
'Accept' => 'application/hal+json',
'Content-Type' => 'application/hal+json; charset=UTF-8',
]);
$apiAddress = 'http://my.project.tld/categories';
$request->setUri($apiAddress);
$request->setMethod('GET');
$client = new Client();
$response = $client->dispatch($request);
$data = $response->getContent();
...并得到像这样的损坏的JSON:
... and get a broken JSON like this:
1f9e <-- What is it?
{"_links...
\u043 <-- What is it?
1a6...
tfoli <-- What is it?
0
该字符串分为五行:
- 第一行:仅
1f9e
- 第二行:第一部分内容
- 3d行:字符串
1a6
- 第四行:第二个内容部分
- 第五行:
0
- 1st line: only
1f9e
- 2nd line: first content part
- 3d line: string
1a6
- 4th line: the second content part
- 5th line:
0
为什么我会得到其他符号/字符串?如何避免这种情况获得有效的JSON输出?
推荐答案
响应对象的getContent()
方法存在问题.它可能不会解码从请求中获取的内容.请在此处中查看.这可能是原因.我可能是错的!
The problem with the getContent()
method of the response object. It may not decode the content it gets in it from the request. Please have a look at here. This might be the reason. I may be wrong!
因此,它的getBody()
方法对请求的内容进行解码工作.因此,请使用此方法代替getContent()
.
So the getBody()
method of it does the decoding job for the content of the request. So please use this method instead of getContent()
.
$data = $response->getBody();
希望这对您有帮助!
这篇关于如何从Zend Framework 3的响应中获取有效的JSON输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文