khakwani 0 Newbie Poster

I want to convert an XML file into SOAP. And then i have to send that SOAP messgae to server. How can i do this.

Thanks in advance

Dani AI

Generated

— the usual pattern is: wrap your XML inside a SOAP envelope that matches the service namespace/operation the server expects, then POST that envelope (either via PHP's SoapClient or with a raw HTTP request such as cURL).

If the service provides a WSDL, use SoapClient and call the operation directly; SoapClient will build the envelope for you and is easier to debug (enable trace => 1). See the PHP SoapClient docs for options and debugging: PHP SoapClient manual.

If no WSDL is available, build an envelope and send it. Important points:

  • Match the SOAP version and namespaces the server expects (SOAP 1.1 vs 1.2).
  • SOAP 1.1 uses Content-Type: text/xml; charset=utf-8 and usually requires a SOAPAction header.
  • SOAP 1.2 uses Content-Type: application/soap+xml and encodes action differently. See the SOAP note for details: SOAP 1.1 specification.

Minimal example (read XML file, wrap and POST with cURL):

$xml = file_get_contents('payload.xml');
$envelope = '<?xml version="1.0" encoding="UTF-8"?>'
  . '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
  . '<soap:Body>' . $xml . '</soap:Body></soap:Envelope>';

$ch = curl_init('https://service.example.com/endpoint');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $envelope);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'Content-Type: text/xml; charset=utf-8',
  'SOAPAction: "urn:YourAction"'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

Troubleshooting tips: validate the server-supplied example/WSDL, compare your envelope with an expected request, enable SoapClient trace or log raw cURL requests/responses, and use DOMDocument/SimpleXML to ensure the XML you inject is well-formed and has correct namespaces.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.