Error in Yii 2.0: Malformed UTF-8 Characters, Possibly Incorrectly Encoded.
1. Error in 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. Check the code below
<?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. Print out $E->GetMessage(), which contains garbled characters. as shown in Figure 1
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: ��֪��������������
4. Edit the code, MB_CONvert_encoding — Convert the encoding of characters. The response status code is: 500, and the response body is: “SQLState[HY000][2002]php_network_getaddresses: getaddrinfo failed: I don’t know about such a host. “. As shown in Figure 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. Preliminary speculation, I don’t know that such a host is returned by the Windows 10 system. It has nothing to do with whether it is Chinese, but has a relationship with the coding of Chinese itself. Because the message is: “failed to open redis DB connection (tcp://localhost:63799, database=57): 10061 – Because the target computer actively refuses to connect.\r\n”, the encoding of unconverted characters can also run normally.

![编辑代码,mb_convert_encoding — 转换字符的编码。响应状态码为:500,响应体为:"SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 不知道这样的主机。 "。](https://www.shuijingwanwq.com/wp-content/uploads/2021/04/2.jpg)