How pass XML from PHP to the Soap WCF service?

960 views Asked by At

I wrote a WCF Soap Service in C#, that accepting XML and parse it to get the value of the fields. I need to call this service in PHP. But the code that I tried in PHP doesn't pass the XML and when I debug in my service dto.xml is empty.

This is how to call it:

  $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 is the service that I am calling:

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;

and this is sample of XML I am passing to the service from PHP:

<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>

1

There are 1 answers

2
Tomaso Albinoni On

I struggled with PHP's SoapClient for a long time. Eventually I ended up using 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
}