没有不值得去解决的问题,也没有不值得去学习的技术!

在 Yii 2.0 中报错:Malformed UTF-8 characters, possibly incorrectly encoded. 的排查分析

打印输出 $e->getMessage(),其中包含乱码。

1、在 Yii 2.0 中报错:Malformed UTF-8 characters, possibly incorrectly encoded.


{
    "name": "Exception",
    "message": "Malformed UTF-8 characters, possibly incorrectly encoded.",
    "code": 5,
    "type": "yii\\base\\InvalidArgumentException",
    "file": "E:\\wwwroot\\pcs-api-feature-base-3.0\\vendor\\yiisoft\\yii2\\helpers\\BaseJson.php",
    "line": 133,
    "stack-trace": [
        "#0 E:\\wwwroot\\pcs-api-feature-base-3.0\\vendor\\yiisoft\\yii2\\helpers\\BaseJson.php(67): yii\\helpers\\BaseJson::handleJsonError(5)",
        "#1 E:\\wwwroot\\pcs-api-feature-base-3.0\\vendor\\yiisoft\\yii2\\web\\JsonResponseFormatter.php(119): yii\\helpers\\BaseJson::encode('SQLSTATE[HY000]...', 320)",
        "#2 E:\\wwwroot\\pcs-api-feature-base-3.0\\vendor\\yiisoft\\yii2\\web\\JsonResponseFormatter.php(104): yii\\web\\JsonResponseFormatter->formatJson(Object(yii\\web\\Response))",
        "#3 E:\\wwwroot\\pcs-api-feature-base-3.0\\vendor\\yiisoft\\yii2\\web\\Response.php(1070): yii\\web\\JsonResponseFormatter->format(Object(yii\\web\\Response))",
        "#4 E:\\wwwroot\\pcs-api-feature-base-3.0\\vendor\\yiisoft\\yii2\\web\\Response.php(337): yii\\web\\Response->prepare()",
        "#5 E:\\wwwroot\\pcs-api-feature-base-3.0\\vendor\\yiisoft\\yii2\\base\\Application.php(392): yii\\web\\Response->send()",
        "#6 E:\\wwwroot\\pcs-api-feature-base-3.0\\api\\web\\index.php(17): yii\\base\\Application->run()",
        "#7 {main}"
    ]
}


2、查看代码如下

<?php
 
namespace api\rests\check_status;
 
use Yii;
use yii\web\HttpException;
use yii\base\Exception;
use yii\data\ActiveDataProvider;
use yii\data\DataFilter;
 
class IndexAction extends \yii\rest\IndexAction
{
    /**
     * @return int|string|ActiveDataProvider|DataFilter|null
     */
    public function run()
    {
        try {
            Yii::$app->db->open();
            if (!Yii::$app->redis->ping()){
                throw new HttpException(500, 'Redis is unavailable.');
            } elseif (!Yii::$app->db->getIsActive()){
                throw new HttpException(500, 'Database is unavailable.');
            } else {
                return 200;
            }
        } catch (Exception $e) {
            $res = Yii::$app->response;
            $res->statusCode = 500;
            return $e->getMessage();
        }
    }
}
 

3、打印输出 $e->getMessage(),其中包含乱码。如图1

打印输出 $e->getMessage(),其中包含乱码。
图1

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: ��֪��������������


4、编辑代码,mb_convert_encoding — 转换字符的编码。响应状态码为:500,响应体为:”SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 不知道这样的主机。 “。如图2

编辑代码,mb_convert_encoding — 转换字符的编码。响应状态码为:500,响应体为:"SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 不知道这样的主机。 "。
图2
<?php
 
namespace api\rests\check_status;
 
use Yii;
use yii\web\HttpException;
use yii\base\Exception;
use yii\data\ActiveDataProvider;
use yii\data\DataFilter;
 
class IndexAction extends \yii\rest\IndexAction
{
    /**
     * @return array|false|int|string|string[]|ActiveDataProvider|DataFilter|null
     */
    public function run()
    {
        try {
            Yii::$app->db->open();
            if (!Yii::$app->redis->ping()){
                throw new HttpException(500, 'Redis is unavailable.');
            } elseif (!Yii::$app->db->getIsActive()){
                throw new HttpException(500, 'Database is unavailable.');
            } else {
                return 200;
            }
        } catch (Exception $e) {
            Yii::$app->response->statusCode = 500;
            $message = mb_convert_encoding($e->getMessage(), "UTF-8", ["UTF-8", "GBK", "GB2312", "BIG5"]);
            return $message;
        }
    }
}

5、初步推测,不知道这样的主机是由 Windows 10 系统返回的。与是否是中文无甚关系,而是与中文本身的编码存在关系。因为 message 为:”Failed to open redis DB connection (tcp://localhost:63799, database=57): 10061 – 由于目标计算机积极拒绝,无法连接。\r\n” 时,未转换字符的编码也是可以正常运行的。

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理