Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Yonibrese's avatar

Error on a XML post request with HTTP Client

Hello

I am trying to send a post request to an API that requires an XML upload. My request looks as follows:

$xml = "
<?xml version='1.0' encoding='utf-8' ?>
<API>
    <Products>
        <ProductCode>$sku</ProductCode>
        <ListPrice>$newPrice</ListPrice>
    </Products>
</API>";
$xml = trim($xml);
$headers = [
    "Content-type" => "application/x-www-form-urlencoded; charset=utf-8;"
];

$request = http::withHeaders($headers)->post($url,$xml);
dd($request->body());

but every time I send a request I get the following XML error from the API.

<?xml version="1.0" encoding="utf-8" ?>\r\n
<ReturnResult>\r\n
  <Success>False</Success>\r\n
  <Message>Data at the root level is invalid. Line 1, position 1.</Message>\r\n
</ReturnResult>

what are the common reasons for this error?

What can I do to solve this error?

Thanks in advance

0 likes
6 replies
tykus's avatar

You will have a new line before the the XML prolog - it must be first in the document whenever present. Try heredocs syntax instead - it works with interpolation as a " would:

$xml = <<<XML
<?xml version="1.0" encoding="utf-8" ?>
<API>
    <Products>
        <ProductCode>{$sku}</ProductCode>
        <ListPrice>{$newPrice}</ListPrice>
    </Products>
</API>
XML;
Yonibrese's avatar

@tykus I tired your fix and it did not work.

I still got the same error

<?xml version="1.0" encoding="utf-8" ?>\r\n
<ReturnResult>\r\n
  <Success>False</Success>\r\n
  <Message>Data at the root level is invalid. Line 1, position 1.</Message>\r\n
</ReturnResult>

any other Ideas?

Yonibrese's avatar

@tykus

According to the documentation its supposed to be:

<?xml version="1.0" charset="utf-8" ?>

which is what I am sending

Snapey's avatar

I would not set much store by the line and position of the error. More interesting to me is 'at the root level' which is the content of the API tag. I think @tykus was expecting you to post a link to their documentation

Yonibrese's avatar

Here is are two links to the documentation for the API I am trying to connect to. The second link is an the xml schema, I am only updating the price of my items so I only used the ProductCode and the ProductPrice tags.

https://volusionhome.my.site.com/knowledgebase/s/article/DeveloperResources

https://helpcenter.volusion.com/s/article/ProductManagementProductsDeveloper

The PHP example is in a section that only users can access, here is a copy of the php example:

<?php
    //  Create the Xml to POST to the Webservice
    $Xml_to_Send = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
    $Xml_to_Send .= "<Volusion_API>";
    $Xml_to_Send .= "<!--";
    $Xml_to_Send .= "    xml input file for \"\"";
    $Xml_to_Send .= "-->";
    $Xml_to_Send .= "</Volusion_API>";

    //  Create the Header
    $url = "https://www.domain.com/net/WebService.aspx?Login=UserEmail&EncryptedPassword=UserPassword&Import=Insert";
    $header  = "POST".$url." HTTP/1.0 \r\n";
    $header .= "MIME-Version: 1.0 \r\n";
    $header .= "Content-type: text/xml; charset=utf-8 \r\n";
    $header .= "Content-length: ".strlen($post_string)." \r\n";
    $header .= "Content-transfer-encoding: text \r\n";
    $header .= "Request-number: 1 \r\n";
    $header .= "Document-type: Request \r\n";
    $header .= "Interface-Version: Test 1.4 \r\n";
    $header .= "Connection: close \r\n\r\n";
    $header .= $Xml_to_Send;

    //  Post and Return Xml
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
    $data = curl_exec($ch);

    //  Check for Errors
    if (curl_errno($ch)){
        print curl_error($ch);
    } else {
       curl_close($ch);
    }

    //  Display the Xml Returned on the Browser
    echo $data;
?>

When I was working with this API and debugging the issue I did try using cURL and the Content-type of "text/xml" like this example and I got a different error that reads:

"No XML Found to Import!"

I found that this error is based on the content type, That is why I am using the application/x-www-form-urlencoded content type.

also before posting the original question I changed the XML a bit so in my code the parent tags are

<Volusion_API>
...
</Volusion_API>

Again thanks for any help that you can give me

Please or to participate in this conversation.