根级别的XML数据无效。第1行,第1位

时间:2015-10-07 18:05:56

标签: php xml

我正在使用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>'; 
?>

非常感谢任何帮助

2 个答案:

答案 0 :(得分:2)

问题是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;

答案 1 :(得分:0)

PHP脚本前面有空格:

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

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