PHP & XML - 如何从这个 XML 在 PHP 中生成一个肥皂请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18572550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:49:52  来源:igfitidea点击:

PHP & XML - How to generate a soap request in PHP from this XML?

phpxmlsoap

提问by Hans Preutz

I am completly new to SOAP operations.

我完全不熟悉 SOAP 操作。

I have been provided with an XML document (SOAP) to get some collection points for a shipping method.

我得到了一个 XML 文档 (SOAP) 来获取一些运输方式的收集点。

From the manual located here:

从位于此处的手册:

http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint

http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint

I can see that I need to use the following SOAP request:

我可以看到我需要使用以下 SOAP 请求:

POST /package/package_1.3/packageservices.asmx HTTP/1.1
Host: privpakservices.schenker.nu
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://privpakservices.schenker.nu/SearchCollectionPoint"

<?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>
    <SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/">
      <customerID>long</customerID>
      <key>string</key>
      <serviceID>string</serviceID>
      <paramID>int</paramID>
      <address>string</address>
      <postcode>string</postcode>
      <city>string</city>
      <maxhits>int</maxhits>
    </SearchCollectionPoint>
  </soap:Body>
</soap:Envelope>

The thing is that i don't know how to send this as a request using PHP, and how to get the response.

问题是我不知道如何使用 PHP 将其作为请求发送,以及如何获得响应。

Any help to pinpoint me in the right direction, is much appreciated.

非常感谢任何帮助我确定正确方向的帮助。

UPDATE

更新

I can read the response data with var_dump. However, I am not able to read individual element data.

我可以使用 var_dump 读取响应数据。但是,我无法读取单个元素数据。

I need to read data as below

我需要读取数据如下

foreach($parser as $row) {
  echo $row->customerID;
  echo $row->key;
  echo $row->serviceID;  
}

回答by Hans Preutz

If anyone should be interested, i have provided the correct answer:

如果有人应该感兴趣,我已经提供了正确的答案:

$soapUrl = "http://privpakservices.schenker.nu/package/package_1.3/packageservices.asmx?op=SearchCollectionPoint";

$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><SearchCollectionPoint xmlns="http://privpakservices.schenker.nu/"><customerID>XXX</customerID><key>XXXXXX-XXXXXX</key><serviceID></serviceID><paramID>0</paramID><address>Riksv?gen 5</address><postcode>59018</postcode><city>Mantorp</city><maxhits>10</maxhits></SearchCollectionPoint></soap12:Body></soap12:Envelope>';

$headers = array(
"POST /package/package_1.3/packageservices.asmx HTTP/1.1",
"Host: privpakservices.schenker.nu",
"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); 
curl_close($ch);

$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);

$parser = simplexml_load_string($response2);

回答by danish dani

following example might help you.

下面的例子可能对你有帮助。

SOAP XML schema

SOAP XML 模式

<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.dataaccess.com/webservicesserver/">
        <x:Header/>
        <x:Body>
            <web:NumberToDollars>
                <web:dNum>10</web:dNum>
            </web:NumberToDollars>
        </x:Body>
    </x:Envelope>

PHP code

PHP代码

  $wsdl = 'http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL';


    try{
        $clinet=new SoapClient($wsdl);

        $ver =array("dNum"=>"2002");
        $quates=$clinet->NumberToDollars($ver);

        var_dump($quates);


    }

    catch(SoapFault $e)
    {
        echo $e->getMessage();
    }