Error 500 when importing subscribers

Hi,
(My first post to this forum.)

I have a client with his contacts in a FileMaker database. When I try to send a (trial) list of subscribers to import, it results in "Failed with code 500".

object(stdClass)#58 (2) {
  ["Code"]=>
  int(500)
  ["Message"]=>
  string(68) "Sorry, we've run into a problem. Please try again or contact support"
}

When I print_r my command it appears to be well-formed, as below:

array (
     array(  
         'EmailAddress' => 'frank@dataprone.com',   
         'Name' => 'Frank Callanan',   
         'CustomFields' => array( 
         array( 
             'Key'=>'Zip Code',
             'Value'=>'04843' 
            ) 
        ) 
    )
     array(  
         'EmailAddress' => 'info@dataprone.com',   
         'Name' => 'Data Pro North East',   
         'CustomFields' => array( 
         array( 
             'Key'=>'Zip Code',
             'Value'=>'04843' 
            ) 
        ) 
    )
)

I've even tried sending the literal code (with and without the CustomFields 'zip code' array), to no avail. The meat of the code I'm working with is:

<?php

require_once 'csrest/csrest_subscribers.php';

$wrap = new CS_REST_Subscribers($listid, $apikey);

# theList contains 'contacts' which contains 'EmailAddress' and 'Zip'
$thelist['contacts'] = array();
$thecommand = "<pre>array (" ;
foreach($records_result->getRecords() as $records_row){ 
$contactid = $records_row->getField('_kf_parent_id') ;
$Name = $records_row->getField('Contact::c_name_or_orgName') ;
$Zip = $records_row->getField('Contact_Address::ZIP') ;
$EmailAddress = $records_row->getField('Contact_Email::email_address') ;
                #
                # Are we on a new contact?
If (empty ($thelist['contacts'][$contactid])) {
                #
                # Store the contact name
    $thelist['contacts'][$contactid]['Name'] = $Name ;
                #
                # Are we on a new email for this contact?
If (empty ($thelist['contacts'][$contactid]['emails'][$EmailAddress])) {
                #
                # Store the contact email
        $thelist['contacts'][$contactid]['EmailAddress'] = $EmailAddress ;
        }
                #
                # Are we on a new CustomField for this contact?
        If (empty ($thelist['contacts'][$contactid]['CustomFields'])) {
                #
                # Append the CustomField to this contact
            $thelist['contacts'][$contactid]['CustomFields']['Zip']['Key'] = 'Zip Code' ;
            $thelist['contacts'][$contactid]['CustomFields']['Zip']['Value'] = $Zip ;
        }
    }
$thecommand = "<pre>".$thecommand .  
"\t array(  
\t\t 'EmailAddress' => "."'". $EmailAddress."',   
\t\t 'Name' => "."'". $Name."',   
\t\t 'CustomFields' => array( 
\t\t array( 
\t\t\t 'Key'=>'Zip Code',
\t\t\t 'Value'=>'".$Zip."' 
\t\t\t) 
\t\t) 
\t)"."</pre>" ;
}
$thecommand = $thecommand .")</pre>";

$result = $wrap->import($thecommand, true);
 ?>

Can anyone suggest how can I make this right?

Thanks,
Frank C.
Maine, USA

jamesd jamesd, 5 years ago

Hey Frank,

The first thing I'd try is to make sure you're using the correct key for custom fields. It looks like you're using the name or label of the custom field "Zip Code" rather than the custom field key.

If you're not sure what the key for a custom field should be, you can always find out by getting the custom fields for a list.

FCallanan, 5 years ago

Thanks, James. I was using the field name rather than the custom key field.  Yet that reference fixed, it still returns the 500 error.
Now I'm wondering about GET vs POST. I'm making this request with a URL (GET method), but I notice in the documentation it's specified as POST. I'm unsure of how I'd put this dynamic array into a POST form...
I'll keep plugging away. Any tips greatly appreciated.

-Frank

NeoVillage, 5 years ago

And I have exact same problem :(

NeoVillage, 5 years ago

It works! I rebooted the server.

FCallanan, 5 years ago
NeoVillage :

It works! I rebooted the server.

Hey, NeoVillage, can you elaborate?

FCallanan, 5 years ago
FCallanan :

Now I'm wondering about GET vs POST. I'm making this request with a URL (GET method), but I notice in the documentation it's specified as POST.

As I read it, the POST method is applied automatically by the wrapper (line 167 of base_classes.php). If that's true, I'm assuming that the array I assemble in my page is passed through the wrapper, which in turn submits it as a POST.
That would mean I don't need to be concerned with POST vs. GET (and should look elsewhere for my error)?
Can anyone confirm that my assumption is true?

-Frank

FCallanan, 5 years ago

I verified that POST is applied by the wrapper.
I noticed a problem with the placement of the commas in my submission array. My new code controls that with a for structure, and loads all subscriber info into one variable, $thecommand.
In this variable I'm trying to format the data as json (a format I've never user before).
I then call 
   $result = $wrap->import($thecommand, true)
This fails with 500.

$contactCount = count ($thelist['contacts']);
# in the FOR statement, condition #2, break condition, in the bracketed statement
# this permits me to append the comma conditionally to all but the last array item
for ($i = 0;  ; $i++)  // 
{ 
    if($i < $contactCount){
        if($i<$contactCount-1){
            $comma = ", ";
            }else{
            $comma = " ";
            }
        $thecommand = $thecommand .  
        " array(  
         'EmailAddress' => "."'".  $thelist['contacts'][$i]['EmailAddress'] ."',   
         'Name' => "."'". $thelist['contacts'][$i]['Name']."',   
         'CustomFields' => array( 
         array( 
         'Key'=>'ZipCode',
         'Value'=>'".  $thelist['contacts'][$i]['CustomFields']['Zip']['Value']  ."' 
        ) 
        ) 
        )".$comma ;
    }else{
        break;
    }    
}
$thecommand = $thecommand .")";

$result = $wrap->import($thecommand, true);
 ?>

However, if I print_r ($thecommand) and insert that resulting verbatim text into the same $result statement, it works.
This points to my formatting as the issue, but darned if I can find it!
-Frank

Jason Jason, 5 years ago

Hi Frank,

The problem here is that you're attempting to post a literal representation of your subscriber collection as the payload of the request - and over complicating the add process.

In the wrapper you downloaded from here https://github.com/campaignmonitor/createsend-php, there is a samples folder, which has the basic add method done for you in import.php. I'd suggest working from this as your baseline, getting a single subscribe working to your list, and then move on to adding multiples.

Hope this gets you on the right track.

FCallanan, 5 years ago
Jason :

The problem here is that you're attempting to post a literal representation of your subscriber collection as the payload of the request - and over complicating the add process.

Hi Jason,
Thanks for the quick response.
Yes, I have been trying to post a literal representation of my subscriber collection. I have been working from the sample "import.php", and I have been nagged by that little voice saying, "Frank, you're making this WAY too complicated!" Yet sending a native PHP associative array clearly doesn't work.
So here's where I need the nudge. The sample re-formats the array from its native PHP. How can I transform the native array to the required format for import'?
I am still missing a piece of the puzzle...
-Frank

FCallanan, 5 years ago

Neophyte that I am, I didn't recognize that the sample used an alternate construct for associative arrays.  I solved it by amending my code like so:

$thecontact = array(
        'EmailAddress' => $EmailAddress ,
        'Name' =>$Name ,
        'CustomFields' => array(
            array(
                'Key' => 'ZipCode',
                'Value' => $Zip
            )
        )
    ) ;
                #
$thelist['contacts'][$i] = $thecontact ;
 }

#  Import $thelist with 'resubscribe' set to True
$result = $wrap->import($thelist['contacts'], true);

-Frank

luismldias, 5 years ago

Hi all,

I am having the exact same issue but with a simpler bit of code and i can«t seem to get around it.

Here's the code:

$wrap = new CS_REST_Subscribers(CM_LIST_ID, CM_API_KEY);
        $subscriber = array(
            'EmailAddress' => $email,
            'Name' => $nome,
            'Resubscribe' => true
        );
        $result = $wrap->add($subscriber);

Can anyone see anything wrong with it?

Regards,

Luis Dias

FCallanan, 5 years ago
luismldias :

Hi all,

I am having the exact same issue but with a simpler bit of code and i can«t seem to get around it.

Here's the code:

$wrap = new CS_REST_Subscribers(CM_LIST_ID, CM_API_KEY);
        $subscriber = array(
            'EmailAddress' => $email,
            'Name' => $nome,
            'Resubscribe' => true
        );
        $result = $wrap->add($subscriber);

Can anyone see anything wrong with it?

Hi Luis,
Maybe try sending the 'true' parameter separately, within the add command, something like this.

$wrap = new CS_REST_Subscribers(CM_LIST_ID, CM_API_KEY);
        $subscriber = array(
            'EmailAddress' => $email,
            'Name' => $nome
        );
        $result = $wrap->add($subscriber , 'true' );

Frank

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