Campaign Monitor API: PHP Wrapper, Error 400 Failed to deserialize

Well here’s a problem that had me scratching my head for a few hours today. I integrated the Campaign Monitor API into a website for a client, by following the instructions here, only to find that it wouldn’t add a new subscriber even though the code I was using was correct.

It insited on giving me the following error:


Failed with code 400

object(stdClass)#117 (2) {

  ["Code"]=>

  int(400)

  ["Message"]=>

  string(112) "Failed to deserialize your request.

Please check the documentation and try again.

Fields in error: subscriber"

}

After trying about 15 ways of passing the data into the API, I posted on the forum and contacted the support team at Campaign monitor…and got no luck from either of these.

That means it was time to roll up my sleeves, get my hands dirty and see what makes this whole thing tick…

I found that the issue was the use of json_encode, which isn’t supported by PHP 5.1.6.

To resolve this, I added this custom json_encode function to the class/services_json.php file, and then modified the serialise function in class/serialisation.php to this:

function serialise($data) {

  if (phpversion() > "5.1.6")

    return json_encode(@CS_REST_SerialiserFactory::check_encoding($data));

  else

    //use the “compatibility mode” json_encode function instead

    return __json_encode(@CS_REST_SerialiserFactory::check_encoding($data));

}

Once this was done, everything worked as expected, so if you’re running anything less than PHP 5.2 and you’re having issues with the Campaign Monitor API you might want to give this a go.