【问题标题】:XML Data at the root level is invalid. Line 1, position 1根级别的 XML 数据无效。第 1 行,位置 1
【发布时间】:2015-10-07 18:05:56
【问题描述】:

我正在使用 Endicia 邮政服务 API,但无法弄清楚为什么会出现此错误。

  <!-- <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault>
<soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code>
<soap:Reason><soap:Text xml:lang="en">
Server was unable to process request. ---&gt; 
Data at the root level is     invalid. Line 1, position 1.</soap:Text>
</soap:Reason><soap:Detail />
</soap:Fault></soap:Body>
</soap:Envelope> --><pre></pre>

这是我正在使用的代码,在返回的错误消息中,它看起来像一个添加的字符,我在代码中没有看到并且尝试重新编写无济于事。

    <?php
$url = "https://elstestserver.endicia.com/LabelService/EwsLabelService.asmx";
$ch = curl_init();

// set the target url
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);

//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'SOAPAction: "Routing"', 'Content-Length: '.strlen($post_string) ));


$data ="SOAPAction:\"https://elstestserver.endicia.com/LabelService/EwsLabelService.asmx\"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">
<soap:Body>
<LabelRequest Test=\"YES\" LabelType=\"Default\" LabelSize=\"4x6\" ImageFormat=\"PDF\">
<RequesterID></RequesterID>
<AccountID></AccountID>
<PassPhrase></PassPhrase>
<MailClass>Priority</MailClass>
<DateAdvance>1</DateAdvance>
<WeightOz>4.1</WeightOz>
<MailpieceShape>Parcel</MailpieceShape>
<Stealth>TRUE</Stealth>
<PartnerCustomerID></PartnerCustomerID>
<PartnerTransactionID></PartnerTransactionID>
<ToName>Janice Dickens</ToName>
<ToAddress1>7 N wilkes Barre Blvd</ToAddress1>
<ToCity>Wilkes Barre</ToCity>
<ToState>PA</ToState>
<ToPostalCode>18702</ToPostalCode>
<ToZIP4></ToZIP4>
<ToDeliveryPoint>Wilkes Barre</ToDeliveryPoint>
<ToPhone>5704898745</ToPhone>
<FromName>Erica Fuentes</FromName>
<ReturnAddress1>4713 Town N country Blvd</ReturnAddress1>
<FromCity>Tampa</FromCity>
<FromState>FL</FromState>
<FromPostalCode>33615</FromPostalCode>
<FromZIP4></FromZIP4>
<FromPhone>8138559852</FromPhone>
</LabelRequest></soap:Body>
</soap:Envelope>";


curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
$data = strstr($result, '<?');

echo '<!-- '. $data. ' -->'; 

$xml_parser = xml_parser_create();

xml_parse_into_struct($xml_parser, $data, $vals, $index);

xml_parser_free($xml_parser);

$params = array();
$level = array();
foreach ($vals as $xml_elem) {
    if ($xml_elem['type'] == 'open') {
        if (array_key_exists('attributes',$xml_elem)) {
            list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
        } else {
        $level[$xml_elem['level']] = $xml_elem['tag'];
        }
    }
    if ($xml_elem['type'] == 'complete') {
        $start_level = 1;
        $php_stmt = '$params';
            while($start_level < $xml_elem['level']) {
                $php_stmt .= '[$level['.$start_level.']]';
                $start_level++;
            }
        $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
        eval($php_stmt);
    }
}
curl_close($ch);
echo '<pre>'; print_r(); echo'</pre>'; 
?>

任何帮助将不胜感激

【问题讨论】:

    标签: php xml


    【解决方案1】:

    问题是 SOAP12 请求和 GET 与 POST。以下代码有效并获得有效响应。

        <?php
    $soapUrl = "http://elstestserver.endicia.com/LabelService/EwsLabelService.asmx";
    $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <soap12:Body>
    <GetPostageLabel xmlns="www.envmgr.com/LabelService">
    <LabelRequest Test="YES" ImageFormat="PDF">
    <RequesterID>l=AccountNumber</RequesterID>
    <AccountID>YourAccountNumber</AccountID>
    <PassPhrase>passphrasestring</PassPhrase>
    <MailClass>Priority</MailClass>
    <DateAdvance>1</DateAdvance>
    <WeightOz>4.1</WeightOz>
    <MailpieceShape>Parcel</MailpieceShape>
    <Stealth>TRUE</Stealth>
    <PartnerCustomerID></PartnerCustomerID>
    <PartnerTransactionID>1234567</PartnerTransactionID>
    <ToName>Albert Thomson</ToName>
    <ToAddress1>10581 Memorial Hwy</ToAddress1>
    <ToCity>Tampa</ToCity>
    <ToState>FL</ToState>
    <ToPostalCode>33615</ToPostalCode>
    <ToZIP4></ToZIP4>
    <ToDeliveryPoint>00</ToDeliveryPoint>
    <ToPhone></ToPhone>
    <FromName>Ellen Gold</FromName>
    <ReturnAddress1>66 Main St</ReturnAddress1>
    <FromCity>Tampa</FromCity>
    <FromState>FL</FromState>
    <FromPostalCode>33615</FromPostalCode>
    <FromZIP4></FromZIP4>
    <FromPhone></FromPhone>
    </LabelRequest>
    </GetPostageLabel>
    </soap12:Body></soap12:Envelope>';
    
    $headers = array(
    "POST /LabelService/EwsLabelService.asmx HTTP/1.1",
    "Host: elstestserver.endicia.com",
    "Content-Type: application/soap+xml; charset=utf-8",
    "Content-Length: ".strlen($xml_post_string)
    ); 
    
    $url = $soapUrl;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $response = curl_exec($ch);
    echo $response;
    

    【讨论】:

      【解决方案2】:

      您的 PHP 脚本前面有空格:

          <?php
      $url = "https://elstestserver.endicia.com/LabelService/EwsLabelService.asmx";
      $ch = curl_init();
      

      删除它们,它应该可以工作。

      【讨论】:

      • 这只是一个复制粘贴错误,代码中没有空格 - 无论如何谢谢。
      猜你喜欢
      • 2018-10-01
      • 1970-01-01
      • 2020-12-19
      • 2011-10-28
      • 1970-01-01
      • 2018-03-30
      • 2013-07-21
      • 1970-01-01
      相关资源
      最近更新 更多