我正在尝试使用 Laravel 在亚马逊卖家帐户中发布产品。但我收到错误: 第 1 行第 1 列处的 XML 解析致命错误:序言中不允许出现内容。序言中不允许出现内容。这是我的代码,所以如果有人可以帮助我解决这个问题。
$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('/^([^\w<]+)</', '<', $requestXml);*/
// $requestXml = trim($requestXml);
$requestXml = preg_replace('/[[:^print:]]/', '', $requestXml);
Log::channel('amazon')->info('Amazon create feed request: ', ['request' => $requestXml]);
$response = Http::withHeaders([
'Content-Type' => 'text/xml; charset=UTF-8'
])->put($url, [
'body' => utf8_encode($requestXml)
]);
我修改了您的代码以验证字符串和隐藏字符,请尝试这个,并且我还删除了 utf8_encode 函数,因为您的 XML 字符串已经采用 UTF-8 格式:
$requestXml = trim($requestXml); // Remove whitespace from the beginning and end of the XML string
$requestXml = preg_replace('/[[:^print:]]/', '', $requestXml);
// Log the XML request after cleaning it
Log::channel('amazon')->info('Amazon create feed request: ', ['request' => $requestXml]);
$response = Http::withHeaders([
'Content-Type' => 'text/xml; charset=UTF-8'
])->put($url, [
'body' => $requestXml
]);