Multiple custom fields / PHP example / Mailbuild = won't work

Any idea how I can send multiple custom fields using the campaign monitor subscribewithcustom.php sample?

I can get it to send a single custom field, which is always the last array I setup.

From the example:

// Adding a standard text type custom field. This also how you would add a number, or single select multi-option field.
        // For the single select, make sure the value you set in the form has been added in Campaign Monitor.
        $title = new SubscriberCustomField();
        $title->Key = '[Title]';
        $title->Value = $_POST['title'];

// Add the title field to an array
        $customfields = array($title);

What I'm trying to do:

// Adding a standard text type custom field. This also how you would add a number, or single select multi-option field.
        // For the single select, make sure the value you set in the form has been added in Campaign Monitor.
        // Role
        $role = new SubscriberCustomField();
        $role->Key = '[Role]';
        $role->Value = $_POST['fJobTitle'];        
        // Add the title field to an array
        $customfields = array($role);    
        
        // Firm Name
        $firmname = new SubscriberCustomField();
        $firmname->Key = '[Firm Name]';
        $firmname->Value = $_POST['FirmName'];        
        // Add the title field to an array
        $customfields = array($firmname);        
        
        //Town / City
        $towncity = new SubscriberCustomField();
        $towncity->Key = '[Town/city]';
        $towncity->Value = $_POST['Town'];        
        // Add the title field to an array
        $customfields = array($towncity);
        
        //County
        $county = new SubscriberCustomField();
        $county->Key = '[County]';
        $county->Value = $_POST['County'];        
        // Add the title field to an array
        $customfields = array($county);
        
        //Postcode
        $postcode = new SubscriberCustomField();
        $postcode->Key = '[Postcode]';
        $postcode->Value = $_POST['postcode'];        
        // Add the title field to an array
        $customfields = array($postcode);
        
        //Country
        $country = new SubscriberCustomField();
        $country->Key = '[Country]';
        $country->Value = $_POST['country'];        
        // Add the title field to an array
        $customfields = array($country);

        //Address 1 
        $address1 = new SubscriberCustomField();
        $address1->Key = '[Address 1]';
        $address1->Value = $_POST['Address1'];        
        // Add the title field to an array
        $customfields = array($address1);

Any assistance would be appreciated.

Thanks.

bb_matt, 8 years ago

Ok, I've managed to get it to work by doing a call for each field which results in rather clunky code, but it works:

// Adding a standard text type custom field. This also how you would add a number, or single select multi-option field.
        // For the single select, make sure the value you set in the form has been added in Campaign Monitor.
        // Role
        $role = new SubscriberCustomField();
        $role->Key = '[Role]';
        $role->Value = $_POST['fJobTitle'];        
        // Add the title field to an array
        $customfields = array($role);    
        
                // Assign our custom field array to our parameter list
        $params->CustomFields = $customfields;
        
        // Make the call
        $result = get_object_vars($client->AddSubscriberWithCustomFields($params));        
                
        $resultCode = current($result)->Code;
        $resultMessage = current($result)->Message;
        
        // If not successful
        if($resultCode > 0)
            print "The following error was returned : " . $resultMessage;
        else
            print "Success";
        
        // Firm Name
        $firmname = new SubscriberCustomField();
        $firmname->Key = '[Firm Name]';
        $firmname->Value = $_POST['FirmName'];        
        // Add the title field to an array
        $customfields = array($firmname);    

        // Assign our custom field array to our parameter list
        $params->CustomFields = $customfields;
        
        // Make the call
        $result = get_object_vars($client->AddSubscriberWithCustomFields($params));        
                
        $resultCode = current($result)->Code;
        $resultMessage = current($result)->Message;
        
        // If not successful
        if($resultCode > 0)
            print "The following error was returned : " . $resultMessage;
        else
            print "Success";        
        
        //Town / City
        $towncity = new SubscriberCustomField();
        $towncity->Key = '[Town/city]';
        $towncity->Value = $_POST['Town'];        
        // Add the title field to an array
        $customfields = array($towncity);
        
                // Assign our custom field array to our parameter list
        $params->CustomFields = $customfields;
        
        // Make the call
        $result = get_object_vars($client->AddSubscriberWithCustomFields($params));        
                
        $resultCode = current($result)->Code;
        $resultMessage = current($result)->Message;
        
        // If not successful
        if($resultCode > 0)
            print "The following error was returned : " . $resultMessage;
        else
            print "Success";
        
        //County
        $county = new SubscriberCustomField();
        $county->Key = '[County]';
        $county->Value = $_POST['County'];        
        // Add the title field to an array
        $customfields = array($county);
        
                // Assign our custom field array to our parameter list
        $params->CustomFields = $customfields;
        
        // Make the call
        $result = get_object_vars($client->AddSubscriberWithCustomFields($params));        
                
        $resultCode = current($result)->Code;
        $resultMessage = current($result)->Message;
        
        // If not successful
        if($resultCode > 0)
            print "The following error was returned : " . $resultMessage;
        else
            print "Success";
        
        //Postcode
        $postcode = new SubscriberCustomField();
        $postcode->Key = '[Postcode]';
        $postcode->Value = $_POST['postcode'];        
        // Add the title field to an array
        $customfields = array($postcode);
        
                // Assign our custom field array to our parameter list
        $params->CustomFields = $customfields;
        
        // Make the call
        $result = get_object_vars($client->AddSubscriberWithCustomFields($params));        
                
        $resultCode = current($result)->Code;
        $resultMessage = current($result)->Message;
        
        // If not successful
        if($resultCode > 0)
            print "The following error was returned : " . $resultMessage;
        else
            print "Success";
        
        //Country
        $country = new SubscriberCustomField();
        $country->Key = '[Country]';
        $country->Value = $_POST['country'];        
        // Add the title field to an array
        $customfields = array($country);
        
                // Assign our custom field array to our parameter list
        $params->CustomFields = $customfields;
        
        // Make the call
        $result = get_object_vars($client->AddSubscriberWithCustomFields($params));        
                
        $resultCode = current($result)->Code;
        $resultMessage = current($result)->Message;
        
        // If not successful
        if($resultCode > 0)
            print "The following error was returned : " . $resultMessage;
        else
            print "Success";

        //Address 1 
        $address1 = new SubscriberCustomField();
        $address1->Key = '[Address 1]';
        $address1->Value = $_POST['Address1'];        
        // Add the title field to an array
        $customfields = array($address1);    
    
    
        // Assign our custom field array to our parameter list
        $params->CustomFields = $customfields;
        
        // Make the call
        $result = get_object_vars($client->AddSubscriberWithCustomFields($params));        
                
        $resultCode = current($result)->Code;
        $resultMessage = current($result)->Message;
bb_matt, 8 years ago

Ok, I've answered my own question here finally - due to my newbie PHP skills, I should've somehow realised that once I'd created the array in the first custom field, I need to add to it, as what I'm effectively doing, is simply overwriting it!


// Adding a standard text type custom field. This also how you would add a number, or single select multi-option field.
        // For the single select, make sure the value you set in the form has been added in Campaign Monitor.
        // Role
        $role = new SubscriberCustomField();
        $role->Key = '[Role]';
        $role->Value = $_POST['fJobTitle'];        
        // Add the title field to an array
        $customfields = array($role);    
        
        // Firm Name
        $firmname = new SubscriberCustomField();
        $firmname->Key = '[Firm Name]';
        $firmname->Value = $_POST['FirmName'];        
        // Add the title field to an array
        $customfields[] = $firmname;
Billee D. Billee D., 7 years ago

I have beat my head against the wall for quite some time on this issue. So I thought that I would post an update to the Campaign Monitor API doc PHP samples.  for the "Subscriber.AddWithCustomFields" method.

Note that this is currently setup for MailBuild using PHP (4 or 5) and the NuSOAP 0.7.2 libraries, but you can modify it to match your Campaign Monitor API settings very easily:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Subscriber.AddWithCustomFields method of the MailBuild API using PHP and NuSOAP 0.7.2</title>
<style type="text/css" media="screen">
body {
 font: 100%/1.8em 'lucida grande', 'helvetica neue', helvetica, arial, sans-serif;
 margin: 1.2em 10em;
}
h1 {
 color: blue;
 text-transform: uppercase;
}
h2 {
 color: blue;
 text-transform: capitalize;
}
label {
 display: block;
 margin-right: .5em;
 text-align: right;
}

.success {
 color: green;
}
.error {
 color: red;
}
</style>
</head>

<body id="index">
<form action="test_mailbuild_api.php" method="post">
    <table cellpadding="2" cellspacing="0" border="0">
        <tr>
            <td><label for="apikey">API Key:</label></td>
            <td>
              <select id="apikey" name="apikey">
                <option selected="selected" value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">Your API Key</option>
              </select>
            </td>
        </tr>
        <tr>
            <td><label for="listid">List ID:</label></td>
            <td>
              <select id="listid" name="listid">
                <option selected="selected" value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">Your List ID</option>
              </select>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td><label for="email">Email Address:</label></td>
            <td><input id="email" name="email" type="text" /></td>
        </tr>
        <tr>
            <td><label for="name">Name:</label></td>
            <td><input id="name" name="name" type="text" /></td>
        </tr>
        <!-- these next two (2) fields will contain our CustomFields data -->
        <tr>
            <td><label for="phone">Phone:</label></td>
            <td><input id="phone" name="phone" type="text" /></td>
        </tr>
        <tr>
            <td><label for="company">Company:</label></td>
            <td><input id="company" name="company" type="text" /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input id="agree_terms" name="agree_terms" type="checkbox" value="1" /> 
            <label for="agree_terms">I agree to the terms and conditions.</label></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Subscribe" /></td>
        </tr>
    </table>    
</form>

<?php
//For more information, visit http://www.campaignmonitor.com/api/Subscriber.AddWithCustomFields.aspx
require_once('nusoap.0.7.2/lib/nusoap.php');

// Declare a class for a custom field.
class SubscriberCustomField
{
     public $Key;
     public $Value;
}

// First check that a post was made, and that the option to receive the newsletter is also checked.
if($_POST && isset($_POST['agree_terms'])) {

    $endpoint   = 'http://obxdesignworks.createsend.com/api/api.asmx?wsdl';//point this to your MB WSDL    
    $namespace  = 'http://api.createsend.com/api/';//the API namespace
    $soapAction = 'http://api.createsend.com/api/Subscriber.AddWithCustomFields';//MB SOAP action
    $operation  = 'Subscriber.AddWithCustomFields';//MB API operation we are performing
    
    // Set the basic API request information.
    $params = array(
                        'ApiKey' => $_POST['apikey'],
                        'ListID' => $_POST['listid'],
                        'Email'  => $_POST['email'],
                        'Name'   => $_POST['name']
                    );        

    
    // Adding a standard text type custom field. This also how you would add a number, or single select multi-option field.
    // For the single select, make sure the value you set in the form has been added in Campaign Monitor.
    $phone = new SubscriberCustomField();
    $phone->Key = '[phone]';
    $phone->Value = $_POST['phone'];
    
    // Add the phone field to an array
    $customfields = array(new soapval('SubscriberCustomField', 'SubscriberCustomField', $phone, false ,''));
    
    $company = new SubscriberCustomField();
    $company->Key = '[Company]';
    $company->Value = $_POST['company'];
    
    // Add the title field to an array
    $customfields[] = new soapval('SubscriberCustomField', 'SubscriberCustomField', $company, false ,'');

    // Assign our custom field array to our parameter list
    $params['CustomFields'] = $customfields;
    
    //if you want to see what we are sending before sending anything just uncomment the following lines:
    /*
    echo '<pre><strong>Parameters Sent to MailBuild</strong><br />==================================================<br />';
    print_r($params);
    echo '==================================================</pre><br /><br />';
    exit;
    */
    
    // Make the call
    $client = new soapclient($endpoint);
    $result = $client->call($operation, $params, $namespace, $soapAction , '', '', 'rpc', 'literal');
    
    $resultCode = $result['Code'];
    $resultMessage = $result['Message'];
    
    // If not successful
    if($resultCode > 0)
        print "The following error was returned : " . $resultMessage;
    else
        print "Success";
    
    // The following code produces the entire service request and response. It may be useful for debugging.
    
    print "<pre>\n";
    print "Request :\n".htmlspecialchars($client->request) ."\n\n";
    print "Response:\n".htmlspecialchars($client->response)."\n";
    print "</pre>";
    
}
?>
</body>
</html>

Save this file as "test_mailbuild_api.php" (or update to the form's action attribute to match your file name) and remember to replace the "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" strings with your API Key and your List ID.

Thanks to bb_matt for the kick in the right direction! :-)


William Dodson
Outer Banks Design Works
http://obxdesignworks.com/
http://obxmailworks.com/

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