【问题标题】:How pass XML from PHP to the Soap WCF service?如何将 XML 从 PHP 传递到 Soap WCF 服务?
【发布时间】:2015-06-08 21:34:00
【问题描述】:

我用 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);

这是我的方法是我正在调用的服务:

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-&gt;addChild('&lt;LoanApp', $LoanApp); 中有一个 addChild 时,实体不会被转义。
  • @TomasoAlbinoni 谢谢抱歉,这是我的错误,但在我的代码中没问题。

标签: c# php xml wcf soap


【解决方案1】:

我与 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
}

【讨论】:

  • 谢谢,你能不能多解释一下,服务方法的名称在哪里,我应该为 soapAction 和 CURLOPT_URL 写什么?也没有SoapCleint?
  • @Alma 也许这很有用:stackoverflow.com/questions/7120586/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 2014-09-24
相关资源
最近更新 更多