SOAP: looks like we got no XML document
When debugging a SoapFault exception with the message "looks like we got no XML document," developers often fixate on whitespace around PHP tags. While that specific issueâoutputting stray characters before <?php or after ?>âis a classic culprit, it is not the only reason the SOAP engine rejects a request. In the scenario described, where a developer using nusoap v1.94 encounters this error despite believing their script is clean, the root cause usually lies in how the server handles output buffering and HTTP headers.
The error message itself is telling: the PHP SOAP extension expects valid XML to be present in the input stream before it can parse the request. If the application sends any outputâincluding whitespace or a blank lineâbefore the Content-Type header is finalized, the SOAP parser interprets this as an empty or malformed document. This is particularly common in legacy scripts where headers are sent dynamically or when mixed with debugging code that echoes variables directly to the browser.
In the provided example using nusoap v1.94, the script defines a simple getStockQuote function and registers it with the server. The logic appears sound: the function returns a string, which is then cast to an xsd:decimal in the WSDL definition. However, the fatal error occurs at line 20, inside the SoapClient call on the consumer side. This indicates that when the client attempts to invoke getStockQuote, the server response it receives does not match the expected XML envelope structure.
The most frequent architectural mistake leading to this specific exception is failing to suppress output before headers are sent. In older PHP versions, which nusoap v1.94 often targets, the HTTP_RAW_POST_DATA variable was used to capture the raw POST body. If the script outputs anythingâwhether it is an empty string or just a newline characterâbefore calling $server->service(), the SOAP engine fails. The parser checks the beginning of the stream for XML declarations; if it finds whitespace instead, it throws the "no XML document" error.
To resolve this without rewriting the entire legacy stack, ensure that no output is generated before the headers are sent. In modern PHP contexts, you might see ob_start() used to capture output and then ob_end_clean() before processing the SOAP request. However, in the nusoap v1.94 example provided, the simplest fix is to verify that there are absolutely no characters before the opening <?php tag and ensure the script does not inadvertently echo whitespace at the end of the file.
Another potential pitfall involves the WSDL registration. The code registers the method with specific types: 'symbol' => 'xsd:string' and 'return' => 'xsd:decimal'. If the client sends a request expecting a decimal return type but the server logic returns a string that cannot be cleanly cast, or if the XML namespace handling differs between the client and server configuration, the parsing can fail early. The error message "looks like we got no XML document" is generic; it often masks underlying mismatches in how the SOAP envelope is constructed versus what the parser expects to find at the very first byte of the response.
If you are building new services today, relying on nusoap v1.94 is generally not recommended due to its age and lack of support for modern PHP versions. Frameworks like Laravel offer robust built-in support for defining API contracts and handling XML or JSON responses, abstracting away these low-level SOAP quirks. For instance, using a framework allows you to define routes that automatically handle header generation and content negotiation, reducing the risk of accidental output buffering issues.
Ultimately, solving this specific error requires strict adherence to HTTP protocol standards regarding headers and body separation. The server must not send any data before declaring its intent via Content-Type. Once the headers are sent, the XML payload follows immediately. If your legacy nusoap script continues to throw this exception, inspect the output with a hex editor or by disabling browser rendering to see if even a single invisible character is breaking the stream.