Integrate API (PHP5) with existing form [[Help!]]

I'm trying to utilize the API (PHP5) to integrate Campaign Monitor with an existing form. So, if user selects to received the newsletter - they are automatically added to the list in Campaign Monitor as well as being saved in our own database. After reading the documentation, I created an 'if' statement for the newsletter checkbox and then added in the example PHP5 code (subscribewithcustom.php) after editing with my own field names, api id etc.

Now when I submit the form, it just hangs, blank screen no error messages. I move the code to the bottom of the script - the form processes ok but nothing happens with the Campaign Monitor addition. What am I missing or doing wrong here? I know conceptually I'm on the right path.

Here's my script:

<?php

    require('DB.class.php');
    
    $db = DB::getInstance();
    
    $Name = addslashes($_REQUEST['Name']);
    $Email = addslashes($_REQUEST['Email']);
    $Street1 = addslashes($_REQUEST['Street1']);
    $Street2 = addslashes($_REQUEST['Street2']);
    $City = addslashes($_REQUEST['City']);
    $State = addslashes($_REQUEST['State']);
    $Zip = addslashes($_REQUEST['Zip']);
    $Country = addslashes($_REQUEST['Country']);
    $Phone = addslashes($_REQUEST['Phone']);
    $Fax = addslashes($_REQUEST['Fax']);
    
    $EmailUpdates = addslashes($_REQUEST['EmailUpdates']);
    $PreviousVisitor = addslashes($_REQUEST['PreviousVisitor']);
    $AdditionalInfo = addslashes(implode(",",$_REQUEST['AdditionalInfo']));
    $VisitingWhen = addslashes(implode(",",$_REQUEST['VisitingWhen']));
    $GroupInfo = addslashes($_REQUEST['GroupInfo']);
    $RelocatingInfo = addslashes($_REQUEST['RelocatingInfo']);
    $HearAbout = addslashes(implode(",",$_REQUEST['HearAbout']));
    $HearAboutOther = addslashes($_REQUEST['HearAboutOther']);
    $Comments = addslashes($_REQUEST['Comments']);
    
    if($EmailUpdates == "Yes" ) {
        
        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['EmailUpdates'])) {

            try {
    
                $client = new SoapClient("http://app.campaignmonitor.com/api/api.asmx?wsdl", array('trace' => 1));

                // Set the basic API request information.
                $params->ApiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                $params->ListID = "xxxxxxxx";
                $params->Email = $_POST['Email'];
                $params->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.
                $title = new SubscriberCustomField();
                $title->Key = '[State]';
                $title->Value = $_POST['State'];
        
                $title = new SubscriberCustomField();
                $title->Key = '[Country]';
                $title->Value = $_POST['Country'];
        
                // Add the title field to an array
                $customfields = array($title);        
        
                // 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";
            
                // The following code produces the entire service request and response. It may be useful for debugging.
                /*
                print "<pre>\n";
                  print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
                  print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
                  print "</pre>";
                  */
        
            } catch (SoapFault $exception) {
                echo $exception;
            }
        }
        
    }
    
    if($GroupInfo == "Yes" ) {
        $Cc = "xxxxxx@xxxxxx.com" . ", " ;
        
    }    
    
    $sql = "INSERT INTO brochure(Name, Email, Street1, Street2, City, State, Zip, Country, Phone, Fax, EmailUpdates, PreviousVisitor, AdditionalInfo, VisitingWhen, GroupInfo, RelocatingInfo, HearAbout, HearAboutOther, Comments)
        VALUES ('${Name}', '${Email}', '${Street1}', '${Street2}', '${City}', '${State}', '${Zip}', '${Country}', '${Phone}', '${Fax}', '${EmailUpdates}', '${PreviousVisitor}', '${AdditionalInfo}', '${VisitingWhen}', '${GroupInfo}', '${RelocatingInfo}', '${HearAbout}', '${HearAboutOther}', '${Comments}')";
        
    $db->insert($sql);
    $db->close();

    /** Insert Redirect for "Thankyou" Message **/
    header("Location: /thank-you.html");

    /** Format Email **/
    $To="xxxxx@xxxxxxxxx.com";
    $From="xxxxxx@xxxxxxxx.com";
    $Subject="New Brochure Request";

    $Message="
    <html xmlns=http://www.w3.org/1999/xhtml>
    <head>
    <style type=text/css>
    <!--
    body {
        font:Verdana, Arial, Helvetica, sans-serif;
        font-size:11px;
        line-height:14px;
        color:#39240c;
    }
    .form-container {
        width:650px;
        border-top:1px solid #CCCCCC;
        padding-bottom:10px;
        margin:2px 0 20px 0;
        background:#EFEFEF;
    }
    .form-container table {
        margin:5px 5px 0 5px;
    }
    .form-container td {
        width:320px;
        padding-right:15px;
        padding-bottom:5px;
    }

    .float-left {
        float:left;
        margin-right:5px;
    }
    -->
    </style>
    </head>

    <body>
    <img src='http://www.grand-county.com/assets/images/header.jpg'>
    <h2>Brochure Request Data</h2>
    <div class=form-container>
      <table cellpadding=0 cellspacing=0 border=0>
        <tr>
          <td><strong>Name:</strong><br />$Name</td>
          <td><strong>E-mail:</strong><br />
          $Email</td>
        </tr>
        <tr>
          <td><strong>Street Address: </strong><br />
    $Street1<br>
    $Street2</td>
          <td><strong>Phone Number:</strong><br />        
            $Phone</td>
        </tr>
        <tr>
          <td><strong>City:</strong><br />
          $City</td>
          <td><strong>Country:</strong><br />
    $Country</td>
        </tr>
        <tr>
          <td colspan=2>
          <div class=float-left><strong>State:</strong><br />$State
        </div>
          <strong>Zip:</strong><br />$Zip</td>
        </tr>
      </table>
    </div>
    <strong>Send me e-mail updates on future events, activities and packages in Grand County?</strong>
    <div class=form-container>
      <table cellpadding=0 cellspacing=0 border=0>
        <tr>
          <td>
          $EmailUpdates      </td>
        </tr>
      </table>
    </div>
    <strong>Previous visitor to Grand County?</strong>
    <div class=form-container>
      <table cellpadding=0 cellspacing=0 border=0>
        <tr>
          <td> $EmailUpdates </td>
        </tr>
      </table>
    </div>
    <strong>I am interested in receiving additional information on?</strong>
    <div class=form-container>
      <table cellpadding=0 cellspacing=0 border=0>
        <tr>
          <td>$AdditionalInfo</td>
        </tr>
      </table>
    </div>
    <strong>I am interested in visiting Grand County during the? </strong>
    <div class=form-container>
      <table cellpadding=0 cellspacing=0 border=0>
        <tr>
          <td>$VisitingWhen</td>
        </tr>
      </table>  
    </div>
    <strong>Please send me information on Groups, Weddings, Retreats and Re Unions? </strong>
    <div class=form-container>
      <table cellpadding=0 cellspacing=0 border=0>
        <tr>
          <td>$GroupInfo</td>
        </tr>
      </table>
    </div>
    <strong>How did you hear about Grand County?</strong>
    <div class=form-container>
      <table cellpadding=0 cellspacing=0 border=0>
        <tr>
          <td>$HearAbout<br>
            $HearAboutOther</td>
        </tr>
      </table>
    </div>
    <strong>Comments:</strong>
    <div class=form-container>
      <table cellpadding=0 cellspacing=0 border=0>
        <tr>
          <td>$Comments</td>
        </tr>
      </table>
    </div>
    </body>
    </html>
    "; 

    /** Recipients **/
    $Headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\nFrom: $From\r\nCc: $Cc\r\nReply-to: $Email\r\n";

    mail($To, $Subject, $Message, $Headers);

?>
simonok, 8 years ago

Did you put your api key in and the list id?

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