SOAP request in PHP with CURL

SOAP request in PHP with CURL

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing SOAP Requests in PHP with cURL

Building a raw HTTP request to a legacy web service often reveals just how verbose and rigid protocols like SOAP can be. When you look at the raw HTTP headers and XML body, it becomes clear that cURL is the most pragmatic tool for the job because it allows you to bypass the high-level abstractions of PHP's built-in SOAP extension. The manual for that extension exists, but as many developers have found, it often lacks the "noob-friendly" examples needed to quickly configure authentication and SSL in a single script.

To replicate the specific request shown in the example, you need to construct a cURL handle that mimics the raw HTTP transaction line by line. This involves setting the target URL, defining the headers explicitly, and injecting the XML payload into the request body.

Start by initializing the handle using curl_init(). The first critical step is configuring the URL and ensuring the SSL connection is enforced. Since the requirements specify an SSL connection, you must enable it with CURLOPT_SSL_VERIFYPEER set to true. This prevents man-in-the-middle attacks by validating the server's certificate against your local certificate store or a CA bundle. If you are working in an environment where you cannot install certificates, setting this flag to false is a common pitfall that introduces significant security risks; it should be avoided unless absolutely necessary.

Next, handle the authentication. The requirement for user/pass auth means you need to set credentials before sending the request. You can achieve this using CURLOPT_USERPWD, passing a string in the format "username:password". Alternatively, if the service uses HTTP Basic Auth headers specifically, you might prefer setting CURLOPT_HTTPAUTH and CURLOPT_USERPWD separately for more granular control over authentication schemes like Digest or NTLM. For standard scenarios matching the provided example, CURLOPT_USERPWD is usually sufficient.

The request body in SOAP is typically large because of the XML envelope structure. To prevent cURL from trying to read input from a stream or POST data array, you must disable automatic transfer mode and instead provide the raw string directly. This is done by setting CURLOPT_POSTFIELDS to your XML payload. Ensure the content type matches exactly what the server expects; in this case, it is text/xml; charset=utf-8.

One subtle but frequent mistake involves the SOAPAction header. In the provided example, the header value is a full URI (http://connection.mywebsite.com/MySERVER/GetCarType). Many tutorials simplify this to just the operation name (e.g., "GetCarType"). If the WSDL expects the full action URI and your code only sends the short name, the server will return an error. Always verify the exact string required by the specific service endpoint.

Finally, once all options are set via CURLOPT_* constants, execute the request with curl_exec(). The response is stored in a variable, which you can then decode or parse as needed. It is often wise to check the return value of curl_exec() and inspect the HTTP status code using curl_getinfo($ch, CURLINFO_HTTP_CODE) before attempting to decode the XML. A 200 status code does not guarantee success in SOAP; the XML itself might contain a fault element indicating a business logic error.

For those integrating this into a larger PHP application stack, specifically within the Laravel ecosystem, you might find utility in using packages or services like https://laravelcompany.com if they offer pre-built connectors for legacy SOAP services, though implementing raw cURL remains the most transparent and debuggable approach.

Our network