Fix for Bad Request 400 errors in CMBase.php

An issue I was having using Subscriber.subscriberAddWithCustomFields in PHP was caused by CMBase.php generating invalid XML. To fix this issue (this will cause any XML/soap requests to fail using PHP) replace the function array2xml on line 820 of CMBase.php with the following:

function array2xml( $arr, $indent = '', $escape = true )
    $buff = '';
    foreach ( $arr as $k => $v )
        if ( !is_array( $v ) )
            $buff .= "$indent<$k>" . ($escape ? htmlentities( $v ) : $v ) . "</$k>\n";
            Encountered a list. The primary difference between the two branches is that
            in the 'if' branch, a $k element is generated for each item in $v, whereas
            in the 'else' branch, a single $k element encapsulates $v.
            if ( isset( $v[0] ) )
                foreach ( $v as $_k => $_v )
                    if ( is_array( $_v ) )
                         $buff .= "$indent<$k>\n" . array2xml( $_v, $indent . "\t", $escape ) . "$indent</$k>\n";
                        $buff .= "$indent<$k>" . ($escape ? htmlentities( $_v ) : $_v ) . "</$k>\n";
                $buff .= "$indent<$k>\n" . array2xml( $v, $indent . "\t", $escape ) . "$indent</$k>\n";
    return $buff;
Ken Ken, 7 years ago

Thanks for that Pete!

Could you tell us what problem you had? Nobody else has fallen foul of any problems (or atleast not told us), and so I'd like to know the specifics of the problem.

We just want to make sure that changing the utf8_encode calls to htmlentities doesn't break anything else.
jrockwell, 7 years ago


I've been pulling my hair out for 3 hours trying to get subscriberAddWithCustomFields with the PHP sample code. I would never get any errors- just nothing saved to CM. But subscriberAdd would work fine.

Making the changes by PeteW has fixed my problem. But you're right, I want to make sure changing the encoding doesn't break anything either.

Can anyone confirm this is an acceptable solution?

I'm using CakePHP btw.

Phil Phil, 7 years ago

Hi jrockwell (and Pete if you're still around),

As Ken said in the earlier post, this isn't something we've seen a lot of. Are you able to help us out with a bit more info? Do you have (for example) a bit of the generated XML which was causing the problem? (make sure you blank out the apiKey!)

Send us a support request if you'd prefer.

Join 200,000 companies around the world that use Campaign Monitor to run email marketing campaigns that deliver results for their business.

Get started for free