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
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: ��֪��������������
4、编辑代码,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” 时,未转换字符的编码也是可以正常运行的。
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/181638.html