Home > Uncategorized > Uncaught SoapFault exception: [Sender] SOAP-ERROR: Encoding: Violation of encoding rules

Uncaught SoapFault exception: [Sender] SOAP-ERROR: Encoding: Violation of encoding rules

btakggn9c

WTF?, why the hell is every PHP developer saying my webservice is returning Uncaught SoapFault exception: [Sender] SOAP-ERROR: Encoding: Violation of encoding rules – and there’s no problem with those using .NET, or parsing the XML response over HTTP GET/POST

Ok, let’s break down the problem.
1. It’s not a problem with PHP,it’s your WSDL, so here’s how to diagnose it.

Take this simple code snippet

$client = new SoapClient(“http://www.regcheck.org.uk/api/reg.asmx?wsdl”);
$params = array (
“RegistrationNumber” => “TNZ6972”,
“username” => “**Your Username**”
);
$response = $client->__soapCall(‘Check’, array($params));
print_r($response);

Now, take a copy of the WSDL, and save it as a text file, so you can edit it easily, i.e.

http://www.regcheck.org.uk/php.wsdl , then using HTML style comments, take out everything you can until you get a minimum working version. – i.e. a call that returns “something“, not all the data, but “something

<s:complexType name=”Vehicle>
<s:element minOccurs=”0 maxOccurs=”1 name=”vehicleJson type=”s:string/>
<s:element minOccurs=”0 maxOccurs=”1 name=”vehicleXml type=”s:string/>
<s:element minOccurs=”0 maxOccurs=”1 name=”numberOfSeats type=”s:string/>
<s:element minOccurs=”0 maxOccurs=”1 name=”carValue type=”s:string/>
<s:element minOccurs=”0 maxOccurs=”1 name=”immobiliser type=”s:string/>
<!– <s:element minOccurs=”0 maxOccurs=”1 name=”vehicleData> … –>

I had a hunch it was the VehicleData object, since it was the most complex type in the response, so I commented that out – and I now had a minimum working version.

After that, I progressively commented out elements within the VehicleData type, until I got my maximum working version – i.e. as much data as possible without it breaking.

At that point, I hit upon this:

<s:element minOccurs=”0″ maxOccurs=”1″ name=”CarModel”> <s:complexType> <s:simpleContent> <s:extension base=”s:integer“> <s:attribute name=”type” type=”s:NCName” /> </s:extension> </s:simpleContent> </s:complexType> </s:element>

Which was the point at which the webservice stopped returning data, and starting throwing the Uncaught SoapFault exception: [Sender] SOAP-ERROR: Encoding: Violation of encoding rules – and it did look odd, since I new that “CarModel” should be a string, not an integer.

Looking at the underlying C# code, the error was obvious

[System.Xml.Serialization.XmlTextAttribute(DataType=”integer“)]
public string Value {

There is no way that Value could both be a string and an integer, so I changed it to

[System.Xml.Serialization.XmlTextAttribute()]
public string Value {

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: