While working on a client project, I came across an interesting issue in the PEAR::XMLRPC2 package. By default, it uses the xmlrpc C extension in the system. Now that is great but the implementation of that library is so slightly different between different platforms. I found that on Ubuntu, that library can't handle utf-8 encoding (if you know how to do that, let me know), and in some other system it's missing all together.

I also found out that you can switch it to use the Php implementation of XML encode/decoding so it should be more cross platform. Great. However, that package doesn't handle the NULL type, which the xmlrpc extension does!

As we already have multiple projects using this and many databases have NULL in it, I don't want to do through every project to ensure we don't send NULLs across, I went ahead and hacked the Php implementation to handle NULL.

Here is what I did (May not be completely correct, if you have better solution, let me know).

STEP 1: Update Value.php
Goto: XML/RPC2/Backend/Php/Value.php. Search for NULL. You will see it explicitly throws an exception. Change the case statment:

case 'NULL': $explicitType = 'null'; break; case 'resource': case 'unknown type':

Then a few lines below, yet another switch-case statement, add the handling of Null:

case 'Null': return ''; default:

That's it for Value.php. Now any independent Nulls will be handled correctly.

STEP 2: Update Struct.php
To avoid Struct calling encode() on a null pointer, goto /XML/RPC2/Backend/Php/Value/Struct.php. Goto encode(), add two lines:

$result .= '<value>'; error_log("element is $element"); if ($element==null) $result .= '<string></string>'; else $result .= ($element instanceof XML_RPC2_Backend_Php_Value) ?

That's it.

Now your xmlrpc will return empty string for NULL.

OK, so null is not empty string right? And one should not try to send null through XML? Correct. That's why on new code, like on the iPhone, I'm coding to avoid sending NULL through XML. But on server code, many many of them, I'm just lazy.

Again, should you have better solution, let me know.