突然正常的项目报这个错误;
因为最近在迁移新的服务器堡垒机UTF-8编码有些问题,一直在调整服务器编码问题;以为是服务器配置造成的;
排查了半天本机一样有问题,本地最近又没有调整过 系统编码应该不会出现此问题;
报错界面:
[0] InvalidArgumentException in Json.php line 39
Malformed UTF-8 characters, possibly incorrectly encoded
- * @throws \Exception
- */
- protected function output($data)
- {
- try {
- // 返回JSON数据格式到客户端 包含状态信息
- $data = json_encode($data, $this->options['json_encode_param']);
- if (false === $data) {
- throw new \InvalidArgumentException(json_last_error_msg());
- }
- return $data;
- } catch (\Exception $e) {
- if ($e->getPrevious()) {
- throw $e->getPrevious();
- }
- throw $e;
- }
最终排查发现不是系统的问题:
是碰巧的一个字段在使用时候:中文截取的问题;
原因:substr 与 mb_substr 的使用问题;
错误的截取中文造成代码异常不能 正常转换;一定要注意!!!
修改前代码:
$val['linked_teacher'] = nameCryptic(substr($val['linked_teacher'],1,4));
修改后代码:
$val['linked_teacher'] = nameCryptic(mb_substr($val['linked_teacher'],1,4));
唉,这个bug 大意了;
--------------------------------------------------------------------
一些其他的答案:
对资源进行转码:
$json = html_entity_decode($source);
$data = json_decode($json,true);
文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。
__________________________________________________________________________________
若有帮助到您,欢迎点击推荐,您的支持是对我坚持最好的肯定(*^_^*)