【问题标题】:php soap:ServerServer was unable to process request. ---> Sequence contains no elementsphp soap:ServerServer 无法处理请求。 ---> 序列不包含任何元素
【发布时间】:2015-11-23 00:30:08
【问题描述】:

我需要使用 CURL 将 XML 作为肥皂请求发送。我使用

创建了 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

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

【问题讨论】:

  • 看看这个答案:stackoverflow.com/questions/7120586/…,也许比较标题?如果直接在浏览器中调用 SOAPAction URL 会发生什么?或者可能需要一些额外的身份验证?
  • 同样的反应。也没有任何改变
  • 您有 wsdl 或所需的 XML 吗?
  • @AwaisQarni 你有没有想过这个问题?我遇到了完全相同的事情(可能在完全相同的网络服务上,基于您的 URL)。
  • 为什么不使用SoapServer

标签: php xml laravel curl soap


【解决方案1】:

使用这个

$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);

【讨论】:

    【解决方案2】:

    这看起来像是架构验证错误。您没有提供定义的链接,但快速搜索显示this documentation page。如果这是您需要查看的 WSDL:https://secure-wms.com/webserviceexternal/contracts.asmx?WSDL

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


    另外,考虑使用SoapClient PHP class

    $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 的转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-30
      • 2015-09-04
      • 2015-02-19
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      相关资源
      最近更新 更多