【发布时间】:2018-03-20 20:21:04
【问题描述】:
所以我正在集成来自 3rd 方公司的 API,我正面临这种奇怪的情况。
我使用以下代码获取端点
$client = $this->client = new Client([
'base_uri' => 'https://xxx.xxxxxxxxxx.com/api/',
'timeout' => 15
]);
$this->requestConfig = [
'auth' => [
'xxxx@xx.xxx',
'xxxxx'
],
'headers' => [
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded'
],
];
$response = $this->client->get($url, $this->requestConfig);
$content = $response->getBody()->getContents();
现在有趣的是,如果我得到 var_dump 内容:
string(66) ""[{\"ExternalId\":\"38\",\"AgencyReference\":\"45436070356676\"}]""
现在我知道这个响应很糟糕,响应类型如果没有设置 json,json 是 URL 编码的,而且一切都不好闻。
我一直在尝试解析这个字符串。
urldecode 也不行。
问题很简单,给出这样的响应,我怎样才能得到一个正常的数组?
目前使用 PHP 7.1
【问题讨论】:
-
语法错误。谢谢
-
不,我的意思是使用 json_last_error 调试您的解码错误 :)
-
看起来你需要在
json_decode-ing 之前去掉斜线。如果你这样做$content = json_decode(stripslashes($response->getBody()->getContents()));会发生什么? -
感谢 N.B,比 preg_replace 更好