首页 > 解决方案 > InvalidArgumentException:格式错误的 UTF-8 字符,可能在文件中错误编码 - Laraval

问题描述

返回 Std 类类型对象时,显示错误。我正在使用 PHP 7 和 Laravel 7.0

错误是从 JsonResponse.php (Illuminate\Http) 的 setData 函数返回使用 DEFAULT_ENCODING_OPTIONS = 15 的 setData 函数。这就是该问题的原因

在此处输入图像描述

我无法解决此问题,但我尝试将 JsonResponse setData 方法更改$this->encodingOptionsJSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE.

现在错误解决了。但我需要另一种方法来解决这个问题。

public function setData($data = [])
{
    $this->original = $data;

    if ($data instanceof Jsonable) {
        $this->data = $data->toJson($this->encodingOptions);
        //$this->data = json_encode($data->jsonSerialize(), JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE);  // LINE NO: 01

    } elseif ($data instanceof JsonSerializable) {

        $this->data = json_encode($data->jsonSerialize(), $this->encodingOptions);
        //$this->data = json_encode($data->jsonSerialize(), $this->encodingOptions);  // LINE NO: 02

    } elseif ($data instanceof Arrayable) {

        $this->data = json_encode($data->toArray(), $this->encodingOptions);
    } else {
        $this->data = json_encode($data, $this->encodingOptions);
        //$this->data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE);  // LINE NO: 03

    }

    if (!$this->hasValidJson(json_last_error())) {
        throw new InvalidArgumentException(json_last_error_msg());
    }

    return $this->update();
}

取消注释第 1,2,3 行错误已解决!我需要另一种方法来解决这个问题。因为,运行后,作曲家安装该更改解除...这是我检索 json 的一部分

 {  "cart": [
        {
            "id": "a9756db0d79a11ea96c7cf86f7c6e0ee",
            "book_id": "81180660d2ed11eaa5299b806f9af290",
            "user_customer_user_id": "437a2b60d23611ea942efb15df219288",
            "status": "ACTIVE",
            "book": {
                "id": "81180660d2ed11eaa5299b806f9af290",
                "user_admin_user_id": "9c80e8006d5b11e9bb9835ecbe2a229e",
                "publisher_id": "7b0d3ac0d6ec11ea8cb15deb159804b0",
                "printed_price": 319,
                "purchase_link": "no",
                "description": "no",
                "pages": -1,
                "rating_value": 0,
                "file_type": "e-pub",
                "revenue_split_type": "OP",
                "book_authors": [
                    {
                        "user_id": "a6d99c206d5b11e9af1a4bbf4f149b08",
                        "reference_no": "XN87397509",
                        "description": null,
                        "search_tags": null,
                        "bank_name": null,
                        "account_number": null,
                        "user": {
                            "id": "a6d99c206d5b11e9af1a4bbf4f149b08",
                            "name": "Anton Dias",
                            "email": "Anton Dias@email.com",
                            "country_code": null,
                            "contact_number": null,
                            "status": "ACTIVE"
                        }
                    },
                    {
                        "user_id": "aa45c0206d5b11e9941ae50cf510f0f3",
                        "reference_no": "EB26705547",
                        "description": null,
                        "search_tags": null,
                        "bank_name": null,
                        "account_number": null,
                        "user": {
                            "id": "aa45c0206d5b11e9941ae50cf510f0f3",
                            "name": "K.G.Karunathilake",
                            "email": "K.G.Karunathilake@email.com",
                            "country_code": null,
                            "contact_number": null,
                            "status": "ACTIVE"
                        }
                    }
                ],
                "book_images": [
                    {
                        "book_id": "81180660d2ed11eaa5299b806f9af290",
                        "type": "LIST",
                        "size": 0.67
                    },
                    {
                        "book_id": "81180660d2ed11eaa5299b806f9af290",
                        "type": "PROFILE",
                        "size": 0.67
                    }
                ],
                "promotions": []
            }
        }
]
}

标签: phpjsonlaravelencodemalformed

解决方案


推荐阅读