I am trying to post product in Amazon Seller account with Laravel. But I am getting error: XML Parsing Fatal Error at Line 1, Column 1: Content is not allowed in prolog. Content is not allowed in prolog. This is my code, so if someone can help me to solve this.

$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' => $requestXml
          ]);
1

There are 1 answers

5
Jenny On

I have revised your code for validating string and hidden characters please try this one and also I have removed utf8_encode function as your XML string is already in 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
]);