PHP与XML - 如何从这个XML生成PHP中的SOAP请求?

7

我完全不了解SOAP操作。

我收到了一个XML文档(SOAP),用于获取一些运输方式的集合点。

从这里的手册中:

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

我能看到我需要使用以下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>

事情是我不知道如何使用PHP发送请求,以及如何获取响应。
任何帮助指引我正确方向的都非常感激。
更新
我可以通过var_dump读取响应数据。然而,我无法读取单个元素数据。
我需要按以下方式读取数据。
foreach($parser as $row) {
  echo $row->customerID;
  echo $row->key;
  echo $row->serviceID;  
}

1
谷歌 PHP SOAP,或者更好的方法是在 PHP 手册中搜索 SOAP。 - cweiske
请在一两天内接受您的答案。这将标记您的问题为已解决。谢谢! - hakre
2个回答

25

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

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

如果您需要在 $xml_post_string 中放置动态值,例如客户 ID,该怎么办? - Hadi Pawar
作为字符串,您可以始终以此方式附加:$xml = '<?xml version="1.0" ..... <maxhits>' . $myVar . '</maxhits>.... ?>'; - Shahzaib Hayat Khan

5
以下示例可能对您有所帮助。
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代码

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接