Sending Raw XML to WSDL Services with SoapClient
When interacting with legacy SOAP services, the default behavior of PHP's SoapClient often clashes with the service provider's expectations. The specific error you are encounteringâSOAP-ERROR: Encoding: object has no 'any' propertyâis a classic symptom of passing an associative array instead of raw XML to a method that expects a string payload.
The WSDL at https://secure.softwarekey.com/solo/webservices/XmlCustomerService.asmx?WSDL defines the CustomerSearch operation with a specific input signature. Based on the documentation and the error trace, this endpoint does not accept standard typed arrays or loosely defined objects. Instead, it requires the SOAP body to contain a raw XML string that matches the schema exactly.
The Root Cause: Array vs. String Mismatch
Your initial attempt constructs an associative array for $CustomerSearch and wraps it in another array keyed by 'xml'. While this approach works perfectly for WSDL services where all input parameters are typed arrays, it fails here because the service expects a single string parameter containing the XML document itself.
When you pass an array like ['xml' => $xml], PHP's SOAP client attempts to map that array onto the expected type definition in the WSDL. If the WSDL expects a xsd:string or a specific complex type that holds raw text, the client tries to instantiate a class for that type. The subsequent error regarding the missing 'any' property usually indicates that the SOAP body structure is malformed relative to what the server's XSD schema demands. The server then processes this malformed request and returns an XML parsing error: -2Invalid Xml Document.
Implementing Raw XML Injection
To resolve this, you must bypass PHP's type mapping for the input parameter entirely. You need to tell the SoapClient instance that it is dealing with raw XML data. This is achieved by setting the xml option in the SoapClient constructor or by explicitly defining the SOAP headers and body structure if the WSDL is auto-generated but not strictly adhered to.
The most direct fix involves passing the raw XML string directly to the method, provided the method signature accepts a string. If the generated stub expects an array wrapper, you may need to use the __soapCall magic method or configure the client with trace enabled to inspect exactly what is being sent.
Here is how you can construct the request using a raw XML string:
$url = 'https://secure.softwarekey.com/solo/webservices/XmlCustomerService.asmx?WSDL';
$client = new SoapClient($url);
// Construct the exact XML payload required by the service
$xmlPayload = sprintf(
'<CustomerSearch>
<AuthorID>%s</AuthorID>
<UserID>%s</UserID>
<UserPassword>%s</UserPassword>
<Email>%s</Email>
</CustomerSearch>',
$authorID,
$userID,
$userPassword,
$customerEmail
);
// Attempt to call the method passing the XML string directly.
// Note: Depending on the specific WSDL stub generation, you might need
// to wrap this in a parameter named 'xml' or pass it as an array with one element.
$result = $client->CustomerSearch([
'xml' => $xmlPayload
]);
// The result object will now contain CustomerSearchResult
if (isset($result['CustomerSearchResult'])) {
echo $result['CustomerSearchResult'];
} else {
// Handle the error response from the server
if (isset($result['any']) && strpos($result['any'], 'Invalid') !== false) {
throw new Exception('Server returned an XML validation error: ' . $result['any']);
}
}
Handling Validation Errors
The documentation snippet provided indicates that the CustomerSearch method supports multiple <SearchField> elements. The example shows empty tags for optional fields, implying a flexible schema where specific fields can be omitted or left empty depending on the search criteria.
If you strictly follow the structure shown in the manual but your variables are undefined, ensure that $authorID, $userID, etc., are populated before insertion into the XML string. If a field is truly optional and should not exist in the payload at all, omitting the tag entirely from the string construction is often safer than passing an empty string, as some strict parsers treat empty strings differently than missing elements.
Why This Matters for Legacy Systems
This pattern of mixing typed arrays with raw XML injection is common in older enterprise services that predate modern RESTful standards or stricter SOAP 1.2 specifications. The SoapClient library is robust but inflexible; it assumes the WSDL contract holds true. When a service deviatesâaccepting a string where an array is expectedâyou must manually enforce the contract via raw XML strings.
By switching to explicit XML construction, you gain control over the exact byte stream sent to the server. This ensures that namespaces, element ordering, and content matching align precisely with what XmlCustomerService.asmx expects, eliminating encoding errors caused by PHP's automatic type inference.