PHP - XML文档结构必须在同一实体中开始和结束

时间:2015-03-13 17:08:57

标签: php xml

我生成XML请求以使用第三方API跟踪订单,如下所示:

/**
 * Method for generating xml request to track orders.
 * 
 * @param $order
 * @return string
 */
private function buildTrackOrderXml($order)
{
    // Init
    $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><ndxml version="2.0"></ndxml>');

    // Add credentials
    $xmlHeader = $xml->addChild('header');
    $xmlHeaderCredentials = $xmlHeader->addChild('credentials');
    $xmlHeaderCredentials->addChild('identity', $this->netDespatch['api_user']);
    $xmlHeaderCredentials->addChild('password', $this->netDespatch['api_password']);

    // Define request
    $xmlRequest = $xml->addChild('request');
    $xmlRequestGettrackrequest = $xmlRequest->addChild('gettrackrequest', '');
    $xmlRequestGettrackrequest->addAttribute('uniqueref', $order->courier_unique_ref);
    $xmlRequestGettrackrequest->addAttribute('consignment', '');
    $xmlRequestGettrackrequest->addAttribute('postalCode', '');
    $xmlRequestGettrackrequest->addAttribute('reference', '');

    // Finished
    return $this->formatXML($xml);
}

/**
 * Method to format SimpleXMLElement element to XML formatted string
 *
 * @param $xml
 * @return string
 */
private function formatXML($xml)
{
    $dom_sxe = dom_import_simplexml($xml);
    $dom_output = new \DOMDocument('1.0');
    $dom_output->formatOutput = true;
    $dom_sxe = $dom_output->importNode($dom_sxe, true);
    $dom_sxe = $dom_output->appendChild($dom_sxe);
    return $dom_output->saveXML($dom_output, LIBXML_NOEMPTYTAG);
}

这将生成以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<ndxml version="2.0">
  <header>
    <credentials>
      <identity>xxx</identity>
      <password>yyy</password>
    </credentials>
  </header>
  <request>
    <gettrackrequest uniqueref="4574z393317" consignment="" postalCode="" reference=""></gettrackrequest>
  </request>
</ndxml>

第三方的API XML解析器不接受此生成的XML请求,我收到以下响应:

<?xml version="1.0" encoding="UTF-8"?>
<ndxml>
  <response>
    <status code="554">XML document structures must start and end within the same entity.</status>
  </response>
</ndxml>

生成的XML请求有什么问题?

1 个答案:

答案 0 :(得分:0)

我喜欢使用以下函数格式在PHP中调用我的XML API:

API.php:

<?php
funtion TemplateAPI()
{
$url = "http://www.website.com";
$request = "<ndxml>
                <header>
                     <credentials>
                          <identity>xxx</identity>
                          <password>yyy</password>
                     </credentials>
                 </header>
                 <request>
                     <gettrackrequest uniqueref="4574z393317" consignment="" postalCode="" reference=""></gettrackrequest>
                 </request>
            </ndxml>"
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => urlencode($request)
)
));

return file_get_contents($url, false, $context);
}
?>

的index.php:

<?php
require_once('API.php');
?>
<html>
    <head>
        <title>API Call</title>
    </head>    
    <body>
        <pre>
            <?php
            echo TemplateAPI();
            ?>
        </pre>
     </body>
</html>