【发布时间】:2019-11-20 17:19:47
【问题描述】:
语言: PHP 7.3 / Laravel 6.*
问题:我的系统收到来自第三方的电子邮件,其中包含有关潜在客户的信息。其中一个向我们发送包含 UTF-16 标签但内容为 UTF-8 的数据。
目标:我想让系统在收到数据时对其进行尝试。然后,如果发生错误,请尝试其他方法。我不想在每个请求上都更改 UTF-16 字符串。
代码:
$input = $request->all();
try {
$xml = simplexml_load_string($input['body-plain']);
throw new Exception();
} catch(Exception $e) {
try {
$input['body-plain'] = str_replace("UTF-16", "UTF-8", $input['body-plain']);
$xml = simplexml_load_string($input['body-plain']);
throw new Exception();
} catch(Exception $e) {
}
}
$prospect = $xml->prospect;
错误:
simplexml_load_string():实体:第 1 行:解析器错误:文档标记为 UTF-16 但包含 UTF-8 内容
try {
$xml = simplexml_load_string($input['body-plain']); //This is the line where the error happen.
throw new Exception();
数据:
...
"body-plain" => """
<?xml version="1.0" encoding="UTF-16"?>
<?ADF version="1.0"?>
<adf>
<prospect>
<id source="language">Français</id>
<requestdate>2019-11-20T11:35:24-05:00</requestdate>
<vehicle interest="buy" status="Used">
...
结论:我不明白为什么当我这样使用 try catch 时它不起作用。
有没有办法告诉 PHP 查看包含和标签匹配?
【问题讨论】:
-
您是否在 Google
Document labelled UTF-16 but has UTF-8 content上搜索过一些结果,看看它们是否有帮助? -
我做到了。大多数解决方案是替换字符串,就像我在第二次尝试代码时所做的那样。这也是我拥有它的原因,但正如我所提到的,我不想对每个请求都这样做。