AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
[https://github.com/top-think/framework/pull/2448/commits/9fc59bf088f269a3b12d455da0e6beee389bd9d3](https://github.com/top-think/framework/pull/2448/commits/9fc59bf088f269a3b12d455da0e6beee389bd9d3) [https://blog.csdn.net/fhl760925513/article/details/114477450](https://blog.csdn.net/fhl760925513/article/details/114477450) 在使用thinkphp6的过程中,尤其是windows本地开发,要是碰到代码运行错误但是死活不报错的情况,可参考以下步骤: 常规步骤: ``` 1. 环境变量配置文件.env中APP_DEBUG设置为true。 ``` APP\_DEBUG=true 2\. 将config目录下的app.php文件中的show\_error\_msg设置为true。 // 显示错误信息 'show\_error\_msg' => true, 如果上面操作无用,查看服务器(我用的是nginx)的错误日志,发现报错:“PHP Fatal error: Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in D:\\phpStudy\\PHPTutorial\\WWW\\tp6\\vendor\\topthink\\framework\\src\\think\\response\\Json.php:50……”。看这个错误意思,再结合源码,这是json\_encode()的时候报错了,字符编码的问题。接下来,非常规步骤,改源码! 非常规步骤: ``` 根据路径找到文件:“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; } ``` 然后在获取服务器信息的地方调用转换: //'Server/Request Data' => $this->app->request->server(), 'Server/Request Data' => $this->changeToUtf8($this->app->request->server()), 这个时候再次发起请求,就可以看到详细的错误信息了! 这个问题曾经困扰了我很久才解决,如果您也碰到了这个问题,且这篇文章帮助到了您,欢迎点赞! 此问题已在tp6的git上pull requests,详情:[https://github.com/top-think/framework/pull/2448。](https://github.com/top-think/framework/pull/2448%E3%80%82)