Search code examples
phpxmlnusoap

PHP NuSOAP generates invalid XML because of nul character in data for xsd:string


I have a web service in PHP, using NuSOAP.

All is well but when data coming from db contains a nul char, e.g. "\x00" this will make NuSOAP to pass it directly into the XML message. I haven't tested but suppose that the Unicode byte order marks "\xff\xfe" and "\xff\xff" will also produce similar behaviour.

I know that if I pre-process my data to remove these sequences before passing it to NuSOAP, this will solve the problem, but this will require adding code to many places which is prone to missing some part or forgetting to implement it for new functions.

My question is if there is a way to tell NuSOAP to escape/remove these sequences by itself.


Solution

  • As everyone around is recommending, using NuSOAP for new projects is not a good idea. But in my case there is lots of legacy code depending on it.

    I could solve my problem by patching the NuSOAP library to discard the nul chars by adding:

            $val = str_replace("\x00", '', $val);
    

    after:

            $val = str_replace('>', '>', $val);
    

    Didn't go deep and add processing of invalid unicode and BOMs.