如何从MailChimp API v3.0批处理操作返回错误

时间:2016-10-19 17:13:05

标签: php mailchimp mailchimp-api-v3.0

我正在努力使用新的MailChimp API和批处理功能,特别是如何从批处理的底层操作中返回任何错误,而不是批处理操作本身。

我的代码如下,并且可以添加两个测试订阅者。响应仅显示整个批次的成功:

[errored_operations] => 0

如果我再次运行它,它将返回类似的响应,但有两个错误:

[errored_operations] => 2

除此之外,没有迹象表明失败或原因。在这种情况下,我们知道这是因为用户已经订阅了。如果我尝试在没有批量调用的情况下添加单个用户,使用POST /lists/{list_id}/members,我会收到一个详细说明失败的详细信息。

stdClass Object
(
    [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
    [title] => Member Exists
    [status] => 400
    [detail] => mary@jackson.net is already a list member. Use PUT to insert or update list members.
    [instance] => 
)

如何在添加(或更新或删除)数百个订阅者时捕获个别错误?

我尝试过循环浏览用户,进行多次单独调用,这样做有效:它会添加用户和/或提供详细的错误报告。但是,当API设置为在单个调用中处理此问题时,进行500次调用似乎很愚蠢。谢谢你的任何想法!

这是我的代码:

$list_id = 'xyz123';
$subscribers = array(
    array(
        'email'     => 'jeff@jackson.net',
        'status'    => 'subscribed',
        'firstname' => 'Jeff',
        'lastname'  => 'Jackson'
    ),
    array(
        'email'     => 'mary@jackson.net',
        'status'    => 'subscribed',
        'firstname' => 'Mary',
        'lastname'  => 'Jackson'
    )
);

$add_subs_batch = add_subs_batch($list_id, $subscribers);
echo '<pre>add_subs_batch: ';
print_r($add_subs_batch);
echo '</pre>';

function add_subs_batch($list_id, $data) {
    $method = 'POST'; 
    $batch_path = 'lists/' . $list_id . '/members';
    $result = mc_request_batch($method, $batch_path, $data);
    if($result && $result->id) {
        $batch_id = $result->id;
        $batch_status = get_batch_status($batch_id);
        return $batch_status;
    }
    else {
        return $result;
    }
}
function get_batch_status($batch_id, $i=1) {
    $method = 'GET'; 
    $target = 'batches/'.$batch_id;
    $result = mc_request($method, $target, $data);
    sleep(1); // wait 1 second and try
    if($result->status == 'finished' ) {
        return $result;
    }
    else {
        return get_batch_status($batch_id, $i+1);
    }
}
function mc_request_batch( $method, $batch_path, $data = false ) {
    $api_key = '12345-us1';
    $dataCenter = substr($api_key,strpos($api_key,'-')+1);
    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/';
    $target = 'batches';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url . $target );
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST' );
    curl_setopt($ch, CURLOPT_TIMEOUT, 10 );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_USERAGENT, 'YOUR-USER-AGENT' );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    if( $data ) {
        $batch_data = new stdClass();
        $batch_data->operations = array();
            foreach ($data as $item) {
                $batch = new stdClass();
                $batch->method = $method;
                $batch->path = $batch_path;
                $batch->body = json_encode( array(
                    'email_address' => $item['email'],
                    'status'        => $item['status'],
                    'merge_fields'  => array( 
                        'FNAME' => $item['firstname'],
                        'LNAME' => $item['lastname']
                    )
                ) );
                $batch_data->operations[] = $batch;
            }

        $batch_data = json_encode($batch_data);
        curl_setopt($ch, CURLOPT_POSTFIELDS,  $batch_data  );
        $response = curl_exec( $ch );
    }
    curl_close( $ch );
    return json_decode($response);
}

1 个答案:

答案 0 :(得分:2)

您将获得id以响应批处理操作。这是“批次ID”,它是唯一标识批量请求的字符串。

要获取批量请求的状态,您必须向网址/batches/{batch_id}调用GET请求。

在回复中,您可以在response_body_url字段中找到一个URL,其中包含批量调用中所有操作结果的gzip压缩文件。

参考:

  

请注意

     

出于安全原因,response_body_url仅在10分钟内有效。   10分钟后,通过GET调用生成另一个   /3.0/batches/{batch_id}。

     

完成批处理操作请求后,可以使用结果   7天。