服务器服务器无法处理请求.——比;序列不包含任何元素


php soap:ServerServer was unable to process request. ---> Sequence contains no elements

我需要使用CURL发送XML作为soap请求。我使用

创建了xml
 $xml = "<?xml .............." blah blah

看起来像

  <?xml version='1.0' encoding='utf-8'?><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><extLoginData xmlns='http://www.JOI.com/schemas/ViaSub.WMS/'><ThreePLKey>4dfdf34</ThreePLKey><Login>abv</Login><Password>abc</Password><FacilityID>1ee</FacilityID><CustomerID>xfs</CustomerID></extLoginData><orders xmlns='http://www.JOI.com/schemas/ViaSub.WMS/'><Order><TransInfo><ReferenceNum>Test</ReferenceNum><PONum>12345</PONum></TransInfo><ShipTo><Name></Name><CompanyName>Peter's Test</CompanyName><Address><Address1>7301 Lennox Ave Unit E3</Address1><Address2></Address2><City>Los Angeles</City><State>CA</State><Zip>90010</Zip><Country>US</Country></Address><PhoneNumber1>858-449-8022</PhoneNumber1><EmailAddress1>lshaules@mercatismedia.com</EmailAddress1><CustomerName>Elizabeth Shaules</CustomerName></ShipTo><ShippingInstructions><Carrier>USPS</Carrier><Mode>First Class Mail</Mode><BillingCode>Prepaid</BillingCode></ShippingInstructions><OrderLineItems><OrderLineItem><SKU>947</SKU><Qualifier>XXX</Qualifier><Qty>1</Qty></OrderLineItem></OrderLineItems></Order></orders></soap:Body></soap:Envelope>

和我使用以下代码发送CURL请求

    $url = 'http://someurl.com/Contracts.asmx';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 120);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'SOAPAction:"http://www.example.com/ViaSub.WMS/CreateOrders"',
        'Content-Type: text/xml; charset=utf-8',
    ));
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $orderXml);
    $result = curl_exec($curl);

但是我得到

soap:ServerServer was unable to process request. ---> Sequence contains no elements

我已经搜索了,但没有找到任何相关的东西。你能告诉我我做错了什么,我是怎么做的吗?

这看起来像是一个模式验证错误。您没有提供到定义的链接,但是快速搜索显示了这个文档页面。如果这是您需要的,请查看WSDL: https://secure-wms.com/webserviceexternal/contracts.asmx?WSDL

Order中查找元素PalletCount。你看它有minOccurs="1",这意味着它是强制性的。但是在您复制的xml字符串中没有这样的元素。这确实是缺少元素的sequence。(可能还有其他一些模式不一致,请仔细查看)。


同时,考虑使用SoapClient PHP类

$WSDL = 'https://secure-wms.com/webserviceexternal/contracts.asmx?WSDL';
$options = [
    'trace' => true,
    'cache' => WSDL_CACHE_NONE,
    'exceptions' => true
];
$client = new SoapClient($WSDL, $options);
$payload = [
    'extLoginData' => [
        'ThreePLKey' => '4dfdf34',
        'Login' => 'abv',
        'Password' => 'abc',
        'FacilityID' => '1ee',
        'CustomerID' => 'xfs'
    ],
    'orders' => [
        'Order' => [
           // etc...
        ]
    ]
]);
$response = $client->CreateOrders($payload);

通过这种方式,客户端处理模式验证、标头设置(不过您可以设置额外的标头)、命名空间和数组到xml的转换。

使用

$xml = "<?xml ..............</soap:Envelope>";
$headers = array(
            "Content-type: text/xml;charset='"utf-8'"",
            "Accept: text/xml",
            "Cache-Control: no-cache",
            "Pragma: no-cache",
            "SOAPAction: http://www.example.com/ViaSub.WMS/CreateOrders", 
            "Content-length: ".strlen($xml),
        ); //SOAPAction: your op URL
$url = 'http://someurl.com/Contracts.asmx';
// PHP cURL  for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// converting
$response = curl_exec($ch); 
curl_close($ch);
var_dump($response);