PHP Wrapper, Error 400 Failed to deserialize (Fields in error: subscr)

I've been trying to use the API to add a new subscriber but keep running into this error message:

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"
}

My PHP code is:

require_once './csrest_subscribers.php';
    
    $wrap = new CS_REST_Subscribers(
      'my_info_here', 
      'my_info_here',
      'http'
    );
    
    $result = $wrap->add(    
    array (
      'EmailAddress' => 'Subscriber email',
      'Name' => 'Subscriber name',
      'CustomFields' => array(
        array(
          'Key' => 'Telephone',
          'Value' => 'Field Value'
        )
      ),
      'Resubscribe' => true
    )
    );
        
    if($result->was_successful()) {
        echo "Subscribed with code ".$result->http_status_code;
    } else {
        echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
        var_dump($result->response);
        echo '</pre>';
    }

If I don't specify that it should use http, it times out and doesn't run, with no error returned. I've tried using realistic data in the fields for $wrap->add but this doesn't make any difference. Pulling my hair out over here! Anyone have any ideas?

JonJoyce, 5 years ago

I seem to be getting a similar response on my wrapper for the update client access call:

http://i56.tinypic.com/xkn5mq.jpg


Jonathon Joyce

http://www.jonathonjoyce.co.uk/projects/campaign-monitor-cfc/
http://campaignmonitorcfc.riaforge.org/
Greg Strutton Greg Strutton, 5 years ago

Hi jsims281,

Just had a quick look over your code.  It looks like your adding two arrays for your custom fields, which maybe causing the problem.  Can you try the following please:

    $result = $wrap->add(    
    array (
      'EmailAddress' => 'Subscriber email',
      'Name' => 'Subscriber name',
      'CustomFields' => array(
          'Key' => 'Telephone',
          'Value' => 'Field Value'
      ),
      'Resubscribe' => true
    )
    );

It might be worth looking into capturing the JSON your passing to the method in the API to make sure it's properly formatted.

@ Jonathon - It might be best if you can do the same.  Make capture the JSON request your passing to the API to make sure it's correctly set up and valid.

I hope that helps.
Greg

jsims281, 5 years ago

Hi thanks for the quick reply,

I tried the code you suggested but I get the same error message as before.

Could you expand on how we would go about capturing the JSON request?

Edit: it's worth noting that this I've tried a number of different ways of setting up the data, including with no custom fields at all and always get the same error

tobyb tobyb, 5 years ago

Hi jsims281,

Your initial code with the double array in custom fields is the correct format (as per the PHP samples your obviously using). If you get in touch with support at campaignmonitor.com with your account details we can have a look at the exact request your making and go from there.

Sorry for the inconvenience,
Toby

jsims281, 5 years ago

Thanks Toby,

Emailed them a few hours ago but no reply yet. When the solution is found I will post back here so that everyone else can refer to it as well.

jsims281, 5 years ago

The issue was I was using PHP 5.1, which doesn't support json_encode.

Solution: I got a json encode function from the net, then altered the code in the serialise function to this:

<code>
    function serialise($data) {
      if (phpversion() > "5.1.6")
        return json_encode(@CS_REST_SerialiserFactory::check_encoding($data));
      else
        //use the “compatibility mode

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
1-888-533-8098