本文内容转载于解决thinkphp6(tp6)在状态码500下不报错,或者显示错误“Malformed UTF-8 characters”的问题
非常规步骤:
根据路径找到文件:“tp6\vendor\topthink\framework\src\think\exception\Handle.php”。
查看源码,找到“Server/Request Data”,发现tp6获取的服务器信息内容非常的详细,如果你的计算机名为中文,或者某些文件夹名包含中文,那就会因为字符编码,而造成json_encode()报错。既然找到原因了,那就解决问题,转下获取内容的编码即可!
在Handle.php的底部加上字符编码转换函数:
/**
* 将获取的服务器信息中的中文编码转为utf-8
* 修复在开启debug模式时出现的Malformed UTF-8 characters 错误
* @access protected
* @param $data array
* @return array 转化后的数组
*/
protected function changeToUtf8(array $data): array
{
foreach ($data as $key => $value) {
$data[$key] = mb_convert_encoding($value, "UTF-8", "GBK, GBK2312");
}
return $data;
}
然后在获取服务器信息的地方调用转换(179行附近):
//'Server/Request Data' => $this->app->request->server(),
'Server/Request Data' => $this->changeToUtf8($this->app->request->server()),
本文做笔记使用,如对你有所帮助,请到原文点赞,谢谢。