Add multiple values to custom field with v3 api

I have an 'area of interest' section on a signup form with multiple selectable checkboxes.

I'm trying to import these into CM using the php wrapper.

I've tried the following php:

        foreach ($areaofinterest as $value)
          {
          $areaofinterestbuilt .= "'" . $value . "', ";
          }               
        $areaofinterestbuilt = substr($areaofinterestbuilt,0,-2);

and then used this

        $result = $cm->subscriberAddWithCustomFields($email, $name, array('AreaofInterest' => array($areaofinterestbuilt), 'Postcode' => $postcode, 'Company' => $company));
       
but no areas of interest are imported into CM.

How should I be doing this?

Phil Phil, 4 years ago

Hi Harvey,

From the looks of this code you're using an ancient wrapper for version 2 of our API.

I would advise that you convert your code over to the version 3 wrapper, which has a sample covering exactly this situation.

Hope that helps :)

harveyf, 4 years ago

Thanks for that Phil, I was sure I was using the V3 API!

I'll use the wrapper you've indicated, but looking at the example you've given I'm still not clear how I go about doing this.

I'm getting an array of area of interests from the signup form, stored in $areaofinterest, how do I then insert these into the 'CustomFields' array?

Phil Phil, 4 years ago

Hi Harvey, you might have been looking at the V3 API docs, but that wrapper is definitely for v2!

Ok, from the sample i mentioned, the key bit is this:

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

In that example, there are two entries in the CustomFields array which have the same 'Key', of 'Multi Option Field 1'. That very closely matches the instruction from the documentation, which says this:
"Select Many custom fields are set by providing multiple Custom Field array items with the same key."

How to do that for your data? I'm thinking something like this:

$CustomFields = array(
    array(
        'Key' => 'Postcode',
        'Value' => $postcode
    ),
    array(
        'Key' => 'Company',
        'Value' => $company
    )
);

// push the AreaOfInterest values into the $CustomFields array
foreach ($areaofinterest as $value)
{
    $CustomFields[] = array(
        'Key' => 'AreaOfInterest',
        'Value' => $value
    );
}
// add the subscriber with no resubscribe
$result = $wrap->add(array(
    'EmailAddress' => $email,
    'Name' => $name,
    'CustomFields' => $CustomFields,
    'Resubscribe' => false)
);

Hope that helps/works :)
Phil

Edit: formatting dramas

harveyf, 4 years ago

Thank you Phil, that worked perfectly!

Oh, just one thing, you have a closing bracket missing on the $CustomFields array, might be worth updating for future viewers.

Thanks again.

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