You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel对接亚马逊卖家平台报错:XML首行首列不允许内容

解决Laravel上传亚马逊商品时的XML解析错误:Content is not allowed in prolog

这个错误的核心原因是XML内容的开头(XML声明<?xml...?>之前)存在多余字符——可能是不可见的UTF-8 BOM、前置空格/换行,或者处理后残留的无效字符,导致亚马逊的XML解析器无法识别合法的XML起始结构。结合你的代码,以下是具体修复方案:

修复步骤

1. 修正XML字符串的前置空白

你构造的XML开头有缩进空格,直接导致解析器认为开头存在无效内容。需要让XML声明顶格写,去掉所有前置空白:

// 原代码中XML声明前的空格全部移除
$requestXml = '<?xml version="1.0" encoding="utf-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
  <!-- 剩余XML内容保持不变 -->
</AmazonEnvelope>';

2. 精准清理无效字符

替换你当前的preg_replace语句,用更精准的规则去除UTF-8 BOM和开头的所有空白字符:

// 移除开头的UTF-8 BOM(如果存在)和所有前置空白
$requestXml = preg_replace('/^\x{EF}\x{BB}\x{BF}|\s+/', '', $requestXml, 1);

3. 修正Laravel Http请求的传参方式

Laravel的Http::put方法如果传入数组,会默认以表单格式发送请求,而非直接传递XML请求体。直接将XML字符串作为第二个参数即可:

$response = Http::withHeaders([
    'Content-Type' => 'text/xml; charset=UTF-8'
])->put($url, $requestXml);

完整修正后代码示例

$requestXml = '<?xml version="1.0" encoding="utf-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
  <Header>
    <DocumentVersion>1.01</DocumentVersion>
    <MerchantIdentifier>Myid</MerchantIdentifier>
  </Header>
  <MessageType>Product</MessageType>
  <PurgeAndReplace>false</PurgeAndReplace>
  <Message>
    <MessageID>1</MessageID>
    <OperationType>Update</OperationType>
    <Product>
      <SKU>56789</SKU>
      <StandardProductID>
        <Type>ASIN</Type>
        <Value>B0EXAMPLEG</Value>
      </StandardProductID>
      <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode>
      <DescriptionData>
        <Title>Example Product Title</Title>
        <Brand>Example Product Brand</Brand>
        <Description>This is an example product description.</Description>
        <BulletPoint>Example Bullet Point 1</BulletPoint>
        <BulletPoint>Example Bullet Point 2</BulletPoint>
        <MSRP currency="USD">25.19</MSRP>
        <Manufacturer>Example Product Manufacturer</Manufacturer>
        <ItemType>example-item-type</ItemType>
      </DescriptionData>
      <ProductData>
        <Health>
          <ProductType>
            <HealthMisc>
              <Ingredients>Example Ingredients</Ingredients>
              <Directions>Example Directions</Directions>
            </HealthMisc>
          </ProductType>
        </Health>
      </ProductData>
    </Product>
  </Message>
</AmazonEnvelope>';

// 清理开头无效字符
$requestXml = preg_replace('/^\x{EF}\x{BB}\x{BF}|\s+/', '', $requestXml, 1);

Log::channel('amazon')->info('Amazon create feed request: ', ['request' => $requestXml]);

// 发送XML请求体
$response = Http::withHeaders([
    'Content-Type' => 'text/xml; charset=UTF-8'
])->put($url, $requestXml);

额外排查建议

  • 查看日志输出的XML内容,确认开头没有任何多余字符(比如空格、特殊符号)
  • 如果XML是从文件读取的,确保文件保存为无BOM的UTF-8格式
  • 长期建议使用XML生成类(如SimpleXMLElement)构造XML,避免手动拼接引入格式错误

内容的提问来源于stack exchange,提问作者Mica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 02:24:59