如何将XML从PHP传递到Soap WCF服务

本文关键字:Soap WCF 服务 PHP XML | 更新日期: 2023-09-27 17:54:33

我用c#编写了一个WCF Soap服务,它接受XML并解析它以获得字段的值。我需要在PHP中调用这个服务。但是我在PHP中尝试的代码没有传递XML,当我在我的服务中调试时,dto.xml是空的。

是这样命名的:

  $wsdl = "http://localhost:525845/cust.svc?wsdl";
  $soap_client = new SoapClient($wsdl);
  $Process = "LOAD";
  $client->soap_defencoding = 'UTF-8';
  $xml = new SimpleXMLElement("<Credit></Credit>");
  $xml->addChild('LoanApp', $LoanApp);
  $xml->addChild('Routing', $Routing);
  $xml->addChild('Processing', $Processing);
  $xml->addChild('Process', $Process);

 $param = array(
'dto' => $xml
);
$result1 = $soap_client->Process_Load($param);
print_r($result1);

this is my method是我正在调用的服务:

public string Process_Load(Model.TransferData dto)
    {
          XmlDocument parsed_xml = new XmlDocument();
        string _byteOrderMarkUtf8 =    Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
        if (dto.xml.StartsWith(_byteOrderMarkUtf8))
        {
            dto.xml = dto.xml.Remove(0, _byteOrderMarkUtf8.Length);
        }
        parsed_xml.LoadXml(dto.xml);
        XmlNodeList xnList = parsed_xml.SelectNodes("/IEZ/Credit/LoanApp/Routing/Processing/Process");
        if (xnList != null)
            Process = xnList.Item(0).InnerText;

这是我从PHP传递给服务的XML示例:

<IEZ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Credit>
    <LoanApp>
    <Routing Transaction="LoanApp">
        <Processing>
            <Process Type="Trans-type">LOAD</Process>
        </Processing>
    </Routing>
  </LoanApp>

如何将XML从PHP传递到Soap WCF服务

我与PHP的SoapClient斗争了很长一段时间。最后我使用Curl:

$xml = new SimpleXMLElement("<Credit></Credit>");
$xml->LoanApp = $LoanApp; // This way any entities in $LoanApp are escaped
$xml->Routing = $Routing;
$xml->Processing = $Processing;
$xml->Process = $Process;
// Omit prolog
$dom = dom_import_simplexml($xml);
$SOAP_data = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
$xml_post_string = '<?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>' . $SOAP_data
    .'</soap:Body></soap:Envelope>';
$headers = array(
    "Content-type: text/xml;charset='"utf-8'"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice", 
    "Content-length: ".strlen($xml_post_string)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, "https://connecting.website.com/soap.asmx?op=DoSomething");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Send SOAP data
if ($response = curl_exec($ch)) {
    // $response contains XML response in string format
}