PHP Batch Import

I've been running batch imports using the sample PHP code for a while now, but errors aren't being handled as expected because the response format isn't what's expected. I'm referring to samples/subscriber/import.php, lines 35 to 48:

echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
    var_dump($result->response);
    echo '</pre>';
   
    if($result->response->TotalExistingSubscribers > 0) {
        echo 'Updated '.$result->response->TotalExistingSubscribers.' existing subscribers in the list';       
    } else if($result->response->TotalNewSubscribers > 0) {
        echo 'Added '.$result->response->TotalNewSubscribers.' to the list';
    } else if(count($result->response->DuplicateEmailsInSubmission) > 0) {
        echo $result->response->DuplicateEmailsInSubmission.' were duplicated in the provided array.';
    }
   
    echo 'The following emails failed to import correctly.<pre>';
    var_dump($result->response->FailureDetails);

$result->response->TotalExistingSubscribers etc are null as the response isn't an object, instead var_dump($result->response) gives (email addresses removed for privacy):

string(2922) "HTTP/1.1 400 Bad Request
Server: csw
Vary: Accept-Encoding
Cache-Control: private, s-maxage=0
X-CS-Node: 303d5e73b046241aeedb3b04f9903092
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
P3P: CP="OTI DSP COR CUR IVD CONi OTPi OUR IND UNI STA PRE"
Date: Thu, 05 Jan 2012 14:58:46 GMT
Connection: close

{"ResultData":{"TotalUniqueEmailsSubmitted":27,"TotalExistingSubscribers":0,"TotalNewSubscribers":0,"DuplicateEmailsInSubmission":[],"FailureDetails":[{"EmailAddress":"xxx xxx@xxx.xxx","Code":1,"Message":"Invalid Email Address"}]},"Code":210,"Message":"Subscriber Import had some failures"}"


Is there a way to properly decode the response so that the demo code works as intended?

tobyb tobyb, 4 years ago

Hi Rhydian,

We're having trouble reproducing this behaviour, can you try updating to the latest version of the php wrapper (1.0.13 available here). If that doesn't fix the problem can you get in touch with support at campaignmonitor.com, including your API Key and we'll have a look into a few things.

Rhydian, 4 years ago

Hi Toby,

Thanks for your help - updating to the latest version fixed the header / decode problem so I'm now able to parse the results correctly.

FYI the example code is still incorrect in subscriber/import.php - $result->response->TotalExistingSubscribers should be $result->response->ResultData->TotalExistingSubscribers etc, as shown below:

    if($result->response->ResultData->TotalExistingSubscribers > 0) {
        echo 'Updated '.$result->response->ResultData->TotalExistingSubscribers.' existing subscribers in the list';       
    } else if($result->response->ResultData->TotalNewSubscribers > 0) {
        echo 'Added '.$result->response->ResultData->TotalNewSubscribers.' to the list';
    } else if(count($result->response->ResultData->DuplicateEmailsInSubmission) > 0) {
        echo $result->response->ResultData->DuplicateEmailsInSubmission.' were duplicated in the provided array.';
    }
   
    echo 'The following emails failed to import correctly.<pre>';
    var_dump($result->response->ResultData->FailureDetails);

Thanks,
Rhydian

tobyb tobyb, 4 years ago

Hey Rhydian,

Good to hear things are working correctly now. I've just updated the import sample on GitHub, thanks for letting us know.

Toby.

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