PHP wrapper and UTF8 strings

Just wanted to post this in case it helps someone. I've noticed before that although Campaign Monitor's API appears to work in UTF-8, that UTF-8 character strings don't make it through correctly when using the provided PHP wrapper. I have a Danish client who needed this to work for obvious reasons, so I finally dug in and found the problem.

Turns out that the PHP wrapper assumes that the strings you feed it are not already UTF-8, and it runs everything through utf8_encode(), and trying to encode a string that's already UTF-8 produces gibberish. If you're working with Wordpress (as I often do), you'll run into this problem as the WP folks were smart enough to use UTF-8 as its charset.

The fix is easy, just remove the offending utf8_encode call in line 176 of CMBase.php:

$postdata .= '&' . $k . '=' .rawurlencode($v);

And then change the array2xml() function on line 492 so that $escape defaults to false:

function array2xml( $arr, $indent = '', $escape =false )
shane, 6 years ago

Also, I haven't seen this mentioned elsewhere, but if your server has PHP built with SOAP enabled, it's probably easier to just use the native SOAP support.

jamesd jamesd, 6 years ago

Hey Shane, are you using the latest code from GitHub?

If so, I believe the encoding problem you mentioned should not occur. I added the fixEncoding function to address this very issue.

Let me know if it's still an issue if you're using the latest code. Thanks.

danstramer, 6 years ago

Where in the code should I update the fixed utf-8 issue?
I am using the Campagin Monitor contact form 7 plugin


danstramer, 6 years ago

Got it, thanks
Just copied all the code from GitHub into the campaignmonitor.php file
the hebrew encoding now works!


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

Get started for free