Warning: simplexml_load_file() [function.simplexml-load-file]:
input conversion failed due to input error, bytes 0xAD 0xB5 0x3C 0x2F
此错误表示 XML 解析器遇到无法按声明编码解码的字节序列。
XML 声明编码与实际内容编码不一致。例如 XML 头声明:
<?xml version="1.0" encoding="GB2312"?>
但文档内部夹杂了 UTF-8 或其他编码的字符(如某些字符实体、外部引入片段),导致 libxml2 解析失败。SimpleXML 底层依赖 libxml2,对编码不匹配零容忍。
读取原始内容,用 iconv 强制转为声明编码(或统一转 UTF-8 再修正声明)。
<?php
$url = 'https://example.com/feed.xml';
$raw = file_get_contents($url);
// 1. 检测/假设实际编码(如检测到 UTF-8 但声明 GB2312)
$actualEncoding = 'UTF-8'; // 可用 mb_detect_encoding($raw, 'UTF-8,GB2312,GBK')
$declaredEncoding = 'GB2312'; // 从 XML 头正则提取
if (strtoupper($actualEncoding) !== strtoupper($declaredEncoding)) {
// 转为声明编码,或统一转 UTF-8 并修改 XML 头
$raw = iconv($actualEncoding, $declaredEncoding . '//IGNORE', $raw);
// 同步修正 XML 头编码声明
$raw = preg_replace('/encoding\s*=\s*["\']?[^"\']+["\']?/i', "encoding=\"{$declaredEncoding}\"", $raw, 1);
}
// 2. 加载转码后的字符串
$xml = simplexml_load_string($raw);
?>
若确知实际编码,直接修改 XML 头匹配实际:
<?php
$content = file_get_contents('feed.xml');
// 实际为 UTF-8,但头声明 GB2312
$content = preg_replace('/encoding\s*=\s*["\']GB2312["\']/i', 'encoding="UTF-8"', $content, 1);
$xml = simplexml_load_string($content);
?>
<?php
libxml_use_internal_errors(true);
$xml = simplexml_load_file('feed.xml', 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);
if ($xml === false) {
$errors = libxml_get_errors();
// 记录错误后尝试方案一/二
libxml_clear_errors();
}
?>
simplexml_load_* 失败,记录原始字节片段(如报错中的 0xAD 0xB5...)便于事后分析。DOMDocument (loadHTML 容错性更强) 或第三方库 masterminds/html5-php。<?php
function detectXmlEncoding($xmlString) {
// 1. 先读 XML 声明
if (preg_match('/encoding\s*=\s*["\']([^"\']+)["\']/i', $xmlString, $m)) {
return strtoupper($m[1]);
}
// 2. 无声明则按 UTF-8 处理 (XML 1.0 默认)
return 'UTF-8';
}
?>