We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

PHP API CustomFieldNN json error

RayC76
Regular Participant

PHP API CustomFieldNN json error

I am trying a very simple integration... 

- test to see if a user exists

- if not, create

- if so, update

 

My code almost mirrors example from CC.

 

I keep getting the following error:

[error_key] => json.regex.mismatch.custom_fields
[error_message] => #/custom_fields/1/name: This attribute value must be of the format 'CustomFieldNN'

 

Funny thing is, I'd like to update the CustomFields, but I'm not even there yet... ;)

 

Suggestions?

 

Thanks!

 

Code:

$cc = new ConstantContact(APIKEY);

try {
// see if the sperson already exists in the account
$response = $cc->getContactByEmail(ACCESS_TOKEN, $_POST['emailAddress']);

// create it if it does not exist and add category

if (empty($response->results)) {
$contact = new Contact();
$contact->addEmail($_POST['emailAddress']);
$contact->addList('1320236928');
$returnContact = $cc->addContact(ACCESS_TOKEN, $contact);
} else {
// update an existing contact
$contact = $response->results[0];
$contact->first_name = 'Jim';
$contact->addList('1320236928');
$returnContact = $cc->updateContact(ACCESS_TOKEN, $contact);
}
// catch any exceptions thrown during the process and print the errors to screen
} catch (CtctException $ex) {
echo '<span class="label label-important">Error '.$action.'</span>';
echo '<div class="container alert-error"><pre class="failure-pre">';
print_r($ex->getErrors());
echo '</pre></div>';
die();
}

13 REPLIES 13
RayC76
Regular Participant

Further trouble shooting shows it works creating and updating contacts if they do not have any custom info.  If they do, it barfs with the above mentioned error.  Dumping out the arrays, it doesn't seem like the array structure is changing any.

 

Another interesting point is that it still barfs when I delete a contact with a custom field and then try to readd it...  apparently the system remembers deleted contacts...

 

Any help is appreciated.

 

Thanks.

The second part of your question is easier to answer - deleted contacts are kept around for a period of time.  In our old CMS, they are kept around forever and never truly deleted.  In our new CMS, which it sounds like you're on, Contacts can be completely deleted if they aren't Unsubscribed (those we keep around forever to honor their unsubscribe) but they are not deleted immediately in case you made a mistake you can then recover from it. 

 

Regarding the first part of your post, we are working on a bug right now around custom fields with our new CMS system.  Unfortunately, it was both unexpected (as most bugs are) and especially complicated.  We are working hard to fix it but as of now, we don't have a very good workaround.  The problem is that the custom_field->name object is coming back as the label that it is configured as (for example "Favorite dog") instead of the number of the field (for example "CustomField1").  As such, it isn't matching the expected format the API needs to map the field correctly. 

 

We will update once we have a fix for this.

Dave Berard
Senior Product Manager, Constant Contact
RayC76
Regular Participant

What is the ETA on resolution and is there a work around?

At this point, the only workaround is to not update the custom fields or not send them over.  We don't really have a generic workaround for how to map these fields as there is no obvious way.  I also don't have an ETA as this is a very complex technical problem that we're still evaluating potential solutons for, there is no clear resolution and we don't want to do something that makes the problem worse. 

Dave Berard
Senior Product Manager, Constant Contact
RayC76
Regular Participant

Upon request, CustomFields are in the contact object.  You can add other custom fields using the PHP API, but there is no way to delete them before resubmiting for update.

 

How do you recommend deleting the CustomFields elements of the array/JSON object using the PHP library?

 

Thanks.

Hello,

 

I've taken a look at your code and the issue you've run into, and the easiest way around this is to simply remove the custom fields from the contact object. In the case of our PHP wrapper, we build the class to mirror the JSON structure for the API and use PHP's json_encode function to generate the payload.

 

Because of this, you can do the following to ignore any custom fields:

$contact = $response->results[0];
$contact->custom_fields = array();

 

Once you do this, you should be able to avoid the naming issue with the custom fields until we have something more available.

 

If you run into any problems with this, plaese let me know and I'll be happy to help!

 

Sincerely,

Elijah G.

API Support Specialist

Elijah G.
API Support Engineer

Has this issue been fixed yet? I am getting the same error when I try to add custom fields

EvanF9
Occasional Participant

^... and the answer is....  Yes or No?

 

Can custom fields be added to the json object/array and passed successfully to CC using the php_sdk?

Hello,

 

This issue was fixed close to the end of 2013 and has been corrected. You can now use the PHP SDK to manage custom fields for contacts and should not have the problem that is described in this tread.

 

If you do experience any issues, please let us know!

 

Best Regards,

Elijah G.
API Support Engineer

I am still having this issue. Do I need to update anything in php-sdk? Below is the response I got when I tried to add new custom field.

 

Error Creating Contact

Array
(
[0] => Array
(
[error_key] => json.regex.mismatch.custom_fields
[error_message] => #/custom_fields/0/name: This attribute value must be of the format 'CustomFieldNN'
)

)

You should not need to update the php-sdk. It may be a problem with the actual names of the custom fields that you are importing, if they do not match the given format. Is it possible to provide the content that you are sending to update the contact? ( I would suggest scrubbing it for personal details before posting, as this is a public forum)

 

When you create a custom field in the PHP SDK, you do need to use the CustomField1-CustomField15 naming convention. While the UI does now allow for any names for custom fields, we are still working on providing that same functionality through our APIs.

 

Best Regards,

Elijah G.
API Support Engineer
EvanF9
Occasional Participant

Ok...  from reading the API docs here...

http://developer.constantcontact.com/docs/contacts-api/contacts-collection.html

and above...

 

the correct way to push custom fields in new CC lists is to json_encode this

 

$custom_fields => array (                
                '0'=> array(
                    "label" => "CustomField1",
                    "name"  =>  "CustomField1",
                    "value" =>  "2"                
                ),
                '1'=> array(
                    "label" => "CustomField2",
                    "name"  =>  "CustomField2",
                    "value" =>  "4"                
                ),
                '2'=> array(
                    "label" => "CustomField3",
                    "name"  =>  "CustomField3",
                    "value" =>  "1"                
                ),
                '3'=> array(
                    "label" => "CustomField4",
                    "name"  =>  "CustomField4",
                    "value" =>  "9"                
                ),

 

so that ....  json_encode ($custom_fields) gives us this.....

 

"custom_fields" =  {                
        '0': {
            "label": "CustomField1",
            "name":  "CustomField1",
            "value": "2"                
        },
        '1': {
            "label": "CustomField2",
            "name":  "CustomField2",
            "value": "4"                
        },
        '2': {
            "label": "CustomField3",
            "name" : "CustomField3",
            "value": "1"                
        },
        '3': {
            "label": "CustomField4",
            "name" : "CustomField4",
            "value":  9"                
        },
    };

 

Holler back if this ain't right !

 

 

 

 

 

Hi Bill,

 

The target for how we want to handle the custom fields for a contact is an array of objects. An example of what the JSON should look like is this:

"custom_fields":[
    {
        "name":"CustomField1",
        "value":"5",
    },
    {
        "name":"CustomField2",
        "value":"29",
    },
    {
        "name":"CustomField3",
        "value":"35",
    },
    {
        "name":"CustomField4",
        "value":"14",
    }
]

 

 

There's a couple ways that you can accomplish this. You can use PHP to build an array list:

$customFields = array(
    (object)array(
        'name' => 'CustomField1',
        'value' => '6'
    ),
    (object)array(
        'name' => 'CustomField2',
        'value' => '54'
    ),
    (object)array(
        'name' => 'CustomField3',
        'value' => '19'
    )
);

json_encode($customFields);

 

The second method would make use of our PHP SDK to simplify the process a bit. Here is what this code looks like:

use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\CustomField;

$contact = new Contact(); $contact->custom_fields[] = CustomField::create( array("name"=>"CustomField1","value"=>"5") ); $contact->custom_fields[] = CustomField::create( array("name"=>"CustomField2","value"=>"78") ); $contact->custom_fields[] = CustomField::create( array("name"=>"CustomField3","value"=>"29") );

 

In addition to this very barebones code, you can find some code examples for how to use the PHP SDK located in the examples folder: https://github.com/constantcontact/php-sdk/tree/master/examples

 

Please let me know if you have any more questions!

 

Best Regards, 

 

 

Elijah G.
API Support Engineer
Developer Portal

View API documentation, code samples, get your API key.

Visit Page