Why "Couldn't Connect to Host" Happens When Sending Raw SOAP via Curl
You are encountering a disconnect between what you expect from curl_exec and how the PHP curl library interprets your request headers, specifically when bypassing standard HTTP behaviors for raw XML payloads. The error message "couldn't connect to host" is misleading in this context because the underlying TCP connection often succeeds; the failure occurs during the TLS handshake or immediately after, before the server processes the body.
Your ASP.NET implementation using MSXML2.ServerXMLHTTP works because it implicitly handles the HTTP request lifecycle more gracefully regarding headers and content negotiation. In your PHP code, you are manually setting Content-Type and calculating a Content-Length. While this is valid for standard HTTP POST requests, there is a specific nuance when dealing with raw XML bodies in older or stricter server environments that causes the "connection" to fail prematurely.
The primary culprit in your provided snippet is the calculation of the Content-Length header alongside the manual setting of CURLOPT_POSTFIELDS. When you pass a string directly to CURLOPT_POSTFIELDS, cURL automatically determines the size of the payload and sets the Content-Length header for you if the Content-Length header is not explicitly set or if it conflicts with automatic detection. However, in your code, you are explicitly calculating the length:
'Content-Length: '.strlen('<soap:Envelope>...</soap:Envelope>')
If the string passed to CURLOPT_POSTFIELDS changes dynamically (which is common in SOAP scenarios where headers or namespaces might vary), a mismatch between the actual byte length of the data sent and the Content-Length header declared in the request can cause the server to reject the connection immediately. While the error message says "couldn't connect," the server often closes the socket upon receiving a malformed header, which manifests as a connection failure to the client.
Furthermore, disabling peer verification with CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST set to false is a significant security trade-off. This allows you to bypass certificate validation errors, which is useful for testing or internal self-signed certificates, but it exposes your application to man-in-the-middle attacks. If the host xxx.yyy.com presents a valid certificate chain, these flags might not be necessary and should ideally be removed in production to ensure data integrity.
A more robust approach involves letting cURL manage the headers unless you have a very specific requirement to override them. Here is how you can refactor your code to eliminate the manual Content-Length calculation and let the library handle the payload size automatically, which often resolves the connection drop:
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "https://xxx.yyy.com:517/zzz.asmx");
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
// Disable SSL verification only if necessary for your specific environment.
// curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_envelope_string); // Pass the variable directly
// Remove manual Content-Length header to let cURL calculate it automatically.
curl_setopt($soap_do, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml; charset=utf-8'
));
$response = curl_exec($soap_do);
if ($response === false) {
$err = 'Curl error: ' . curl_error($soap_do);
} else {
// Process $response
}
curl_close($soap_do);
By removing the explicit Content-Length calculation, you ensure that cURL sends a header that matches the exact byte count of the data in $soap_envelope_string. This alignment prevents the server from aborting the connection due to a header-body mismatch. Additionally, if you are using Laravel or a similar framework, consider utilizing their built-in HTTP client or LaravelCompany resources for standardized SOAP handling, which abstracts these low-level cURL intricacies and provides better error reporting.
If the issue persists after removing the manual length calculation, inspect the server logs at xxx.yyy.com. The "couldn't connect" error from PHP often masks a 400 Bad Request or a 403 Forbidden response that closes the stream immediately upon receipt of the malformed header.